struts2中获取request文件流

public class InterfaceServiceImplAction extends BaseFormBean{
 private Map dataMap;
 private File file;
 private String fileFileName;
 private String appId;
 private DocBizManageBo docBizManageBo;
 private DocManageBo docManageBo;
 
 public String getEipAttachment4debt() {
  dataMap = new HashMap();
  ActionContext.initialize(ServletActionContext.getRequest(), ServletActionContext.getResponse());
  HttpServletRequest request = ServletActionContext.getRequest();
  if(!ServletFileUpload.isMultipartContent(request)) {
   dataMap.put("code", "1");
   dataMap.put("data", "");
   return SUCCESS;
  }
  try {
   String path = UploadUtil.getFileUploadPath()+File.separator+"thirdReport4debt";
   String fileName = appId+"-三方调查报告."+fileFileName.split("\\.")[1];
   File paramFile = new File(path+File.separator+fileName);
   FileUtils.copyFile(file, paramFile);
   createAttachment(appId, fileName, path);
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  } catch (IOException e1) {
   e1.printStackTrace();
  } catch (Exception e) {
   e.printStackTrace();
  }
//  ServletContext servletContext = ServletActionContext.getServletContext();
//  MultipartResolver multipartResolver = new CommonsMultipartResolver(servletContext);
//  MultipartHttpServletRequest multipartHttpServletRequest = multipartResolver.resolveMultipart(request);
//  MultiValueMap multiFileMap = multipartHttpServletRequest.getMultiFileMap();
//  Map fileMap = multipartHttpServletRequest.getFileMap();
//  List files = multipartHttpServletRequest.getFiles("files");
//  MultipartFile file = multipartHttpServletRequest.getFile("file");
//  FileItemFactory fileItemFactory = new DiskFileItemFactory();
//  ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
//  try {
//   List parseRequest = servletFileUpload.parseRequest(request);
for(FileItem fileItem : parseRequest) {
//    if(!fileItem.isFormField()) {
//     String path = request.getSession().getServletContext().getRealPath("");
//     File paramFile = new File("d:\\Users\\zhangyxi\\Desktop\\",fileItem.getName());
//     fileItem.write(paramFile);
//    }
//   }
//  } catch (FileUploadException e) {
//   e.printStackTrace();
//  } catch (Exception e) {
//   e.printStackTrace();
//  }
  
//  try {
//   InputStream inputStream = fileMap.get("file").getInputStream();
//  } catch (IOException e) {
//   e.printStackTrace();
//  }
//  Iterator iterator = multipartHttpServletRequest.getFileNames();
//  while(iterator.hasNext()) {
//   System.out.println(iterator.next());
//  }
  dataMap.put("code", "0");
  dataMap.put("data", "");
  return SUCCESS;
 }

你可能感兴趣的:(java)