Extjs上传文件

J2EE 码拜 9年前 (2015-04-08) 1200次浏览 0个评论
 

ssh项目,前台用的是extjs,选择文件后跳转到action把文件上传到服务器上。
问题是,我在action中获取的只是一个客户端文件的路径,可以通过路径上传吗? 
我试了一下,报打不开文件路径,可能是把本地路径当成服务器路径了,在服务器端找不到文件。
这个该怎么修改?
附带码

String rootPath = this.getServlet().getServletContext().getRealPath("/"); // 当前项目所在绝对路径
		String sp = rootPath + "DBupload\";
		String uploadname = "";
		File file = new File(sp);
		if (!file.exists()) {
			file.mkdirs();
		}


		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");// 设置日期格式
			String newdate = df.format(new Date());

			uploadname = sp + newdate + ".xls";   //服务器保存路径
			fos = new FileOutputStream(uploadname);
			fis = new FileInputStream(new File(pa));  // pa 为客户端上传的文件路径
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			if (fos != null) {
				fos.close();
			}
			if (fis != null) {
				fis.close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}




Extjs上传文件
10分
你上传的时候,其实文件流已经在request里面了,你用通过request.getInputStream()来获取文件流!
Extjs上传文件
10分

public String fileUpload() throws Exception {
		realPath = ServletActionContext.getServletContext().getRealPath("/pages/filehandle/file");
		System.out.println("文件名: "+fileFileName);
		System.out.println("文件类型: "+fileContentType);
		System.out.println("保存路径: "+realPath);
		if (file != null) {         
			File savefile = new File(new File(realPath), fileFileName);  
			if (!savefile.getParentFile().exists()){              
					savefile.getParentFile().mkdirs();    
				}
			    FileUtils.copyFile(file, savefile);     
		}
		return SUCCESS;
	}

Extjs上传文件
5分
路过帮顶
Extjs上传文件
15分
struts2就是先给你放到服务器磁盘了,接收用的是java.io.File,只能是本地啊。可以保存,没问题的
Extjs上传文件
引用 4 楼 yys79 的回复:

struts2就是先给你放到服务器磁盘了,接收用的是java.io.File,只能是本地啊。可以保存,没问题的

我用的是struts1,该怎么处理?

Extjs上传文件
引用 1 楼 littlebrain4solving 的回复:

你上传的时候,其实文件流已经在request里面了,你用通过request.getInputStream()来获取文件流!

这样获取是空的,后台是struts1 


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Extjs上传文件
喜欢 (0)
[1034331897@qq.com]
分享 (0)

文章评论已关闭!