UploadFilesAction.java
上传用户:itkeji
上传日期:2017-12-04
资源大小:6184k
文件大小:4k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. /*
  2.  * Generated by MyEclipse Struts
  3.  * Template path: templates/java/JavaClass.vtl
  4.  */
  5. package com.techmore.struts.action;
  6. import java.io.File;
  7. import java.util.ArrayList;
  8. import java.util.Iterator;
  9. import java.util.List;
  10. import java.util.UUID;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import org.apache.commons.fileupload.FileItem;
  14. import org.apache.commons.fileupload.FileItemFactory;
  15. import org.apache.commons.fileupload.disk.DiskFileItemFactory;
  16. import org.apache.commons.fileupload.servlet.ServletFileUpload;
  17. import org.apache.struts.action.ActionForm;
  18. import org.apache.struts.action.ActionForward;
  19. import org.apache.struts.action.ActionMapping;
  20. import org.apache.struts.actions.MappingDispatchAction;
  21. /**
  22.  * MyEclipse Struts Creation date: 05-18-2007
  23.  * 
  24.  * XDoclet definition:
  25.  * 
  26.  * @struts.action validate="true"
  27.  */
  28. public class UploadFilesAction extends MappingDispatchAction {
  29. /*
  30.  * Generated Methods
  31.  */
  32. /**
  33.  * Method execute
  34.  * 
  35.  * @param mapping
  36.  * @param form
  37.  * @param request
  38.  * @param response
  39.  * @return ActionForward
  40.  */
  41. public ActionForward upload(ActionMapping mapping, ActionForm form, HttpServletRequest request,
  42. HttpServletResponse response) {
  43. // TODO Auto-generated method stub
  44. System.out.println("xxx");
  45. try {
  46. request.setCharacterEncoding("UTF-8");
  47. FileItemFactory factory = new DiskFileItemFactory();
  48. ServletFileUpload upload = new ServletFileUpload(factory);
  49. // upload.setHeaderEncoding(request.getCharacterEncoding());
  50. System.out.println("request:" + request);
  51. List items = upload.parseRequest(request);
  52. Iterator iter = items.iterator();
  53. while (iter.hasNext()) {
  54. FileItem item = (FileItem) iter.next();
  55. if (item.isFormField()) {/*
  56.  * String name = item.getFieldName(); String value = item.getString();
  57.  * System.out.println("name = " + name); System.out.println("value = " +
  58.  * value); System.out.println("/r/n");
  59.  */
  60. } else {
  61. String fieldName = item.getFieldName();
  62. String fileName = item.getName();
  63. System.out
  64. .println("文件类型   " + fileName.substring(fileName.lastIndexOf('.') + 1, fileName.length()));
  65. // boolean isInMemory = item.isInMemory();
  66. long sizeInBytes = item.getSize();
  67. getListInSession(request).add(fileName);
  68. getListInSession(request).add(UUID.randomUUID());
  69. // String[] str=fieldName.split("\.");
  70. // int l=str.length-1;
  71. // System.out.println("文件类型 "+str[l]);
  72. //
  73. System.out.println("fieldName = " + fieldName);
  74. System.out.println("fileName = " + fileName);
  75. // System.out.println("文件存储路径 "+request.getRealPath("/")+"uploadfile/");
  76. // 路径 D:Program Filesapache-tomcat-6.0.18webappsrockontrol_oaUploadFiles2009042991039312
  77. String filePath = this.getServlet().getServletContext().getRealPath("/") + "uploadfile/" + "中文/";
  78. File savePath = new File(filePath);
  79. System.out.println(savePath.getAbsolutePath());
  80. if (!savePath.exists()) {
  81. savePath.mkdir();
  82. }
  83. item.write(new File(filePath + fileName));
  84. }
  85. }
  86. System.out.println("-------- final--------");
  87. } catch (Exception ex) {
  88. ex.printStackTrace();
  89. }
  90. return null;
  91. }
  92. public ActionForward getResult(ActionMapping mapping, ActionForm form, HttpServletRequest request,
  93. HttpServletResponse response) throws Exception {
  94. List list = getListInSession(request);
  95. request.getSession().removeAttribute("_result");
  96. response.setCharacterEncoding("utf-8");
  97. response.getWriter().print(list);
  98. response.flushBuffer();
  99. return null;
  100. }
  101. public List getListInSession(HttpServletRequest request) {
  102. List result = (List) request.getSession().getAttribute("_result");
  103. if (result == null) {
  104. result = new ArrayList();
  105. request.getSession().setAttribute("_result", result);
  106. }
  107. return result;
  108. }
  109. }