| | |
| | | package com.lf.server.service.all; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.lf.server.controller.all.BaseController; |
| | | 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.sys.AttachEntity; |
| | | import com.lf.server.entity.sys.UserEntity; |
| | | import com.lf.server.helper.FileHelper; |
| | | import com.lf.server.helper.PathHelper; |
| | | import com.lf.server.helper.StringHelper; |
| | | import com.lf.server.helper.WebHelper; |
| | | import com.lf.server.service.sys.AttachService; |
| | | import com.sun.xml.internal.fastinfoset.tools.XML_SAX_StAX_FI; |
| | | import org.apache.commons.codec.digest.DigestUtils; |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.ServletContext; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | 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 static long MAX_FILE_SIZE = 20 * 1024 * 1024; |
| | | 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); |
| | | |
| | | private static final String NO_FILE = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.NOT_FOUND, "文件找不到")); |
| | | |
| | | /** |
| | | * 上传文件 |
| | | */ |
| | | public ResponseMsg<String> upload(UserEntity ue, String tab, MultipartFile file, BaseController ctrl) { |
| | | try { |
| | | if (file == null && file.isEmpty()) { |
| | | return ctrl.fail("文件上传为空", null); |
| | | } |
| | | if (file.getSize() > MAX_FILE_SIZE) { |
| | | return ctrl.fail(String.format("文件大于 %d MB", MAX_FILE_SIZE / 1024 / 1024), null); |
| | | if (file.getSize() > SettingData.MAX_FILE_SIZE) { |
| | | return ctrl.fail(String.format("文件大于 %d MB", SettingData.MAX_FILE_SIZE / 1024 / 1024), null); |
| | | } |
| | | |
| | | // 传输文件 |
| | |
| | | return ctrl.success("文件已存在", md5); |
| | | } |
| | | |
| | | String uploadPath = pathHelper.getUploadFullPath(); |
| | | String targetPath = uploadPath + File.separator + md5; |
| | | AttachEntity ae = getAttachEntity(ue, tab, oldName, md5); |
| | | String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath(); |
| | | newFile.renameTo(new File(targetPath)); |
| | | |
| | | AttachEntity ae = getAttachEntity(ue, tab, oldName, md5); |
| | | int rows = attachService.insert(ae); |
| | | |
| | | return rows > 0 ? ctrl.success(md5) : ctrl.fail("保存文件失败", null); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 上传文件 |
| | | * |
| | | * @param req |
| | | * @param res |
| | | * 下载文件 |
| | | */ |
| | | public void upload(HttpServletRequest req, HttpServletResponse res) { |
| | | public void download(String guid, HttpServletRequest req, HttpServletResponse res, BaseController ctrl) { |
| | | try { |
| | | if (StringHelper.isEmpty(guid)) { |
| | | WebHelper.write2Page(res, NO_FILE); |
| | | return; |
| | | } |
| | | |
| | | AttachEntity entity = attachService.selectByGuid(guid); |
| | | if (entity == null) { |
| | | WebHelper.write2Page(res, NO_FILE); |
| | | return; |
| | | } |
| | | |
| | | setDownloadResponse(entity, res); |
| | | String filePath = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath(); |
| | | |
| | | // 通过response对象,获取到输出流 |
| | | ServletOutputStream outputStream = res.getOutputStream(); |
| | | // 定义输入流,通过输入流读取文件内容 |
| | | FileInputStream fileInputStream = new FileInputStream(filePath); |
| | | |
| | | int len = 0; |
| | | byte[] bytes = new byte[1024]; |
| | | while ((len = fileInputStream.read(bytes)) != -1) { |
| | | // 通过输入流读取文件数据,然后通过上述的输出流写回浏览器 |
| | | outputStream.write(bytes, 0, len); |
| | | outputStream.flush(); |
| | | } |
| | | |
| | | // 关闭资源 |
| | | outputStream.close(); |
| | | fileInputStream.close(); |
| | | } catch (Exception ex) { |
| | | try { |
| | | String msg = JSON.toJSONString(new ResponseMsg<String>(HttpStatus.ERROR, "文件下载出错")); |
| | | WebHelper.write2Page(res, msg); |
| | | } catch (Exception e) { |
| | | log.error(e.getMessage() + e.getStackTrace() + "\n"); |
| | | } |
| | | log.error(ex.getMessage() + ex.getStackTrace() + "\n"); |
| | | } |
| | | } |
| | | |
| | | private void setDownloadResponse(AttachEntity entity, HttpServletResponse res) throws IOException { |
| | | String fileName = URLEncoder.encode(entity.getName(), "UTF-8"); |
| | | |
| | | // 设置响应头中文件的下载方式为附件方式,以及设置文件名 |
| | | res.setHeader("Content-Disposition", "attachment; filename=" + fileName); |
| | | // 设置响应头的编码格式为UTF-8 |
| | | res.setCharacterEncoding("UTF-8"); |
| | | |
| | | // 通过response对象设置响应数据格式(如:"text/plain; charset=utf-8") |
| | | String ext = FileHelper.getExtension(entity.getName()); |
| | | String mime = FileHelper.getMime(ext); |
| | | res.setContentType(mime); |
| | | } |
| | | |
| | | /** |
| | | * 上传文件 |
| | | */ |
| | | 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"); |
| | | } |
| | | } |
| | | } |