struts接收url文件使用upload.parseRequest 获取文件为空的问题

  1. 参考 http://blog.csdn.net/jimmy609/article/details/43648475
  2. public void upload3(HttpServletRequest request) {  
  3.         DiskFileItemFactory factory = new DiskFileItemFactory();  
  4.         ServletFileUpload upload = new ServletFileUpload(factory);  
  5.         try {  
  6.             List<FileItem> list = upload.parseRequest(request);  
  7.             for(FileItem item : list){  
  8.                 if(item.isFormField()){  
  9.                       
  10.                 }else{  
  11.                     //item.write(new File(""));  
  12.                 }  
  13.             }  
  14.         } catch (FileUploadException e) {  
  15.             e.printStackTrace();  
  16.         }  
  17.           
  18.     }
将request重新转换成MultiPartRequestWrapper 如下

public String updateUserPhoto(){
String userId = request.getParameter("mobile");
String fileName = null;
ServletContext context = request.getSession().getServletContext();
String temp = context.getRealPath("/")+"temp";
System.out.println("temp="+temp);

String imgPath = context.getRealPath("/")+"image";
System.out.println("imgPath is "+imgPath);

MultiPartRequestWrapper wrapper = (MultiPartRequestWrapper) request;
request = (HttpServletRequest)wrapper.getRequest();

Enumeration<String> names = wrapper.getFileParameterNames();
while(names.hasMoreElements())
        {
fileName=(String)names.nextElement();
           System.out.println(fileName);
        }


File []files = wrapper.getFiles(fileName);
for(int i=0; i<files.length;i++)
        {
           
           System.out.println(files[i].getName());
           if(uploadFile(files[i],imgPath+"/",fileName)!=ERROR){
           User user = userService.getUserInfoById(userId);
           if(user!=null){
        user.setUserId(userId);
        user.setUserPhoto(imgPath+"\\"+fileName);
           }
        Result result = userService.updateUser(user);
        if(result.flag) {
        returnDomain.setResultMsg(result.msg);
        returnDomain.setStatus("0");
        System.out.println("用户修改头像结果:"+result.msg);
        } else {
        returnDomain.setResultMsg(result.msg);
        returnDomain.setStatus("-1");
        System.out.println("用户修改头像结果:"+result.msg);
        }
           }
        }

return SUCCESS;
}

private String uploadFile(File file,String savePath,String saveName){
FileOutputStream fos = null;
FileInputStream fis = null;
File saveFile = null;

try{
saveFile = new File(savePath);
if(!saveFile.exists() && !saveFile.isDirectory()){
saveFile.mkdir();
}
saveFile = new File(savePath,saveName);
if(!saveFile.exists())
saveFile.createNewFile();


fos = new FileOutputStream(saveFile.getAbsolutePath());
fis = new FileInputStream(file);

byte[] buffer = new byte[1024];
int len = 0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}

}catch(Exception e){
System.out.println("文件上传失败!");
e.printStackTrace();
return ERROR;
}finally{

try {
if(fos!=null)
fos.close();
if(fis!=null)
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return saveFile.getAbsolutePath();


}

你可能感兴趣的:(struts接收url文件使用upload.parseRequest 获取文件为空的问题)