| | |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.net.URLEncoder; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | |
| | |
| | | |
| | | @Autowired |
| | | AttachService attachService; |
| | | |
| | | private final static double D92 = 92; |
| | | |
| | | private final static long ONE_DAY = 24 * 60 * 60 * 1000; |
| | | |
| | | private final static long SIZE_MAX = 1024 * 1024 * 1024 * 1024; |
| | | |
| | | private final static long FILE_SIZE_MAX = 256 * 1024 * 1024 * 1024; |
| | | |
| | | private final static Log log = LogFactory.getLog(FileService.class); |
| | | |
| | |
| | | |
| | | /** |
| | | * 上传文件 |
| | | * |
| | | * @param req |
| | | * @param res |
| | | */ |
| | | public void upload(HttpServletRequest req, HttpServletResponse res) { |
| | | public void uploadData(HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | // 处理中文乱码问题 |
| | | req.setCharacterEncoding("utf-8"); |
| | | res.setContentType("text/html;charset=utf-8"); |
| | | |
| | | // 检查请求是/否是multipart/form-data类型 |
| | | // 检查请求是/否为multipart/form-data类型 |
| | | if (!ServletFileUpload.isMultipartContent(req)) { |
| | | throw new RuntimeException("表单的enctype属性不是multipart/form-data类型!!"); |
| | | throw new RuntimeException("表单的enctype属性不是multipart/form-data类型"); |
| | | } |
| | | |
| | | // 创建上传所需要的两个对象:磁盘文件对象+文件上传对象 |
| | |
| | | ServletFileUpload sfu = new ServletFileUpload(factory); |
| | | ServletRequestContext ctx = new ServletRequestContext(req); |
| | | |
| | | //限制单个文件的大小 |
| | | sfu.setFileSizeMax(1024 * 10); |
| | | |
| | | //限制上传的总文件大小 |
| | | sfu.setSizeMax(1024 * 200); |
| | | |
| | | // 限制上传的总文件大小 |
| | | sfu.setSizeMax(SIZE_MAX); |
| | | // 限制单个文件的大小 |
| | | sfu.setFileSizeMax(FILE_SIZE_MAX); |
| | | // 设置编码方式 |
| | | sfu.setHeaderEncoding("utf-8"); |
| | | |
| | | // list容器用来保存表单中的所有数据信息 |
| | | List<FileItem> items = sfu.parseRequest(ctx); |
| | | |
| | | // 遍历容器,处理解析的内容:一个处理普通表单域,一个处理文件的表单域 |
| | | for (FileItem item : items) { |
| | | if (item.isFormField()) { |
| | |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取临时路径 |
| | | */ |
| | | public String getTempPath() { |
| | | String tempName = FileHelper.getTempPath(); |
| | | String tempPath = pathHelper.getConfig().getTempPath(); |
| | | String path = tempPath + File.separator + tempName; |
| | | |
| | | File file = new File(path); |
| | | if (!file.exists() && !file.isDirectory()) { |
| | | file.mkdirs(); |
| | | } |
| | | |
| | | double ran = Math.random() * 99; |
| | | if (ran > D92) { |
| | | deleteOldPath(); |
| | | } |
| | | |
| | | return path; |
| | | } |
| | | |
| | | public void deleteOldPath() { |
| | | try { |
| | | Long time = System.currentTimeMillis(); |
| | | for (int i = 1, c = 30; i < c; i++) { |
| | | Date date = new Date(time - i * ONE_DAY); |
| | | String str = StringHelper.YMD__FORMAT.format(date); |
| | | |
| | | // |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | } |
| | | } |
| | | } |