| | |
| | | import com.lf.server.entity.all.HttpStatus; |
| | | import com.lf.server.entity.all.ResponseMsg; |
| | | import com.lf.server.entity.all.SettingData; |
| | | import com.lf.server.entity.ctrl.FileInfo; |
| | | import com.lf.server.entity.sys.AttachEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.FileHelper; |
| | |
| | | import java.io.FileInputStream; |
| | | import java.io.IOException; |
| | | import java.net.URLEncoder; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.UUID; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * 数据上传服务类 |
| | |
| | | |
| | | @Autowired |
| | | AttachService attachService; |
| | | |
| | | private final static double D92 = 92; |
| | | |
| | | private final static long ONE_DAY = 24 * 60 * 60 * 1000; |
| | | |
| | |
| | | |
| | | // 传输文件 |
| | | String oldName = file.getOriginalFilename(); |
| | | String filePath = pathHelper.getConfig().getTempPath() + File.separator + oldName; |
| | | String filePath = pathHelper.getTempPath() + File.separator + oldName; |
| | | File newFile = new File(filePath); |
| | | file.transferTo(newFile); |
| | | |
| | |
| | | |
| | | // list容器用来保存表单中的所有数据信息 |
| | | List<FileItem> items = sfu.parseRequest(ctx); |
| | | // 遍历容器,处理解析的内容:一个处理普通表单域,一个处理文件的表单域 |
| | | for (FileItem item : items) { |
| | | if (item.isFormField()) { |
| | | handleFormField(item); |
| | | } else { |
| | | handleFileField(item, req); |
| | | } |
| | | copeFileItems(items, req); |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理解析内容:处理普通表单域和文件表单域 |
| | | */ |
| | | private void copeFileItems(List<FileItem> items, HttpServletRequest req) throws Exception { |
| | | Map<String, String> map = new HashMap<String, String>(3); |
| | | List<FileInfo> list = new ArrayList<FileInfo>(); |
| | | |
| | | String path = pathHelper.getTempPath(); |
| | | for (FileItem item : items) { |
| | | if (item.isFormField()) { |
| | | String key = item.getFieldName(); |
| | | String value = item.getString("utf-8"); |
| | | map.put(key, value); |
| | | continue; |
| | | } |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | |
| | | FileInfo fi = copeFile(item, path, req); |
| | | if (fi != null) { |
| | | list.add(fi); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理 普通数据项 |
| | | * |
| | | * @param item |
| | | * 处理文件 |
| | | */ |
| | | private void handleFormField(FileItem item) { |
| | | // 获取 普通数据项中的 name值 |
| | | String fieldName = item.getFieldName(); |
| | | |
| | | // 获取 普通数据项中的 value值 |
| | | String value = ""; |
| | | private FileInfo copeFile(FileItem item, String path, HttpServletRequest req) { |
| | | try { |
| | | // 以 utf-8的编码格式来解析 value值 |
| | | value = item.getString("utf-8"); |
| | | // 获取文件名,判断是否合法 |
| | | FileInfo fi = new FileInfo(item.getName()); |
| | | if (StringHelper.isEmpty(fi.getFileName())) { |
| | | return null; |
| | | } |
| | | |
| | | fi.setSize(item.getSize()); |
| | | fi.setPath(path + File.separator + fi.getFileName()); |
| | | item.write(new File(fi.getPath())); |
| | | |
| | | return fi; |
| | | } catch (Exception ex) { |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | return null; |
| | | } |
| | | |
| | | // 输出到控制台 |
| | | System.out.println("fieldName:" + fieldName + "--value:" + value); |
| | | } |
| | | |
| | | /** |
| | | * 处理 文件数据项 |
| | | * |
| | | * @param item |
| | | */ |
| | | private void handleFileField(FileItem item, HttpServletRequest req) { |
| | | // 获取 文件数据项中的 文件名 |
| | | String fileName = item.getName(); |
| | | |
| | | // 判断 此文件的文件名是否合法 |
| | | if (fileName == null || "".equals(fileName)) { |
| | | return; |
| | | } |
| | | |
| | | // 控制只能上传图片 |
| | | String img = "image"; |
| | | if (!item.getContentType().startsWith(img)) { |
| | | return; |
| | | } |
| | | |
| | | // 将文件信息输出到控制台:文件名+文件大小 |
| | | System.out.println("fileName:" + fileName); |
| | | System.out.println("fileSize:" + item.getSize()); |
| | | |
| | | // 获取 当前项目下的 /files 目录的绝对位置 |
| | | ServletContext ctx = req.getSession().getServletContext(); |
| | |
| | | item.write(new File(file.toString(), UUID.randomUUID() + "_" + fileName)); |
| | | } catch (Exception e) { |
| | | 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"); |
| | | } |
| | | } |
| | | } |