| | |
| | | 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.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.List; |
| | | import java.util.UUID; |
| | | |
| | |
| | | |
| | | 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.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); |
| | |
| | | } |
| | | |
| | | /** |
| | | * ä¸è½½æä»¶ |
| | | */ |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | | * ä¸ä¼ æä»¶ |
| | | * |
| | | * @param req |