- 参考 http://blog.csdn.net/jimmy609/article/details/43648475
- public void upload3(HttpServletRequest request) {
- DiskFileItemFactory factory = new DiskFileItemFactory();
- ServletFileUpload upload = new ServletFileUpload(factory);
- try {
- List<FileItem> list = upload.parseRequest(request);
- for(FileItem item : list){
- if(item.isFormField()){
- }else{
- //item.write(new File(""));
- }
- }
- } catch (FileUploadException e) {
- e.printStackTrace();
- }
- }
将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;
}
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();
}
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();
}