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();


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值