| | |
| | | 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"); |
| | | } |
| | | } |
| | | } |