当前位置: 代码迷 >> J2EE >> Extjs上传文件解决方案
  详细解决方案

Extjs上传文件解决方案

热度:82   发布时间:2016-04-17 23:36:07.0
Extjs上传文件
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();
}





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


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;
}


------解决思路----------------------
路过帮顶
------解决思路----------------------
struts2就是先给你放到服务器磁盘了,接收用的是java.io.File,只能是本地啊。可以保存,没问题的
  相关解决方案