¶Ô±ÈÐÂÎļþ |
| | |
| | | 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 org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.File; |
| | | |
| | | /** |
| | | * ä¸ä¼ éä»¶æå¡ç±» |
| | | * @author WWW |
| | | */ |
| | | @Service |
| | | public class UploadAttachService { |
| | | @Autowired |
| | | PathHelper pathHelper; |
| | | |
| | | @Autowired |
| | | AttachService attachService; |
| | | |
| | | private final static Log log = LogFactory.getLog(UploadAttachService.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() > SettingData.MAX_FILE_SIZE) { |
| | | return ctrl.fail(String.format("æä»¶å¤§äº %d MB", SettingData.MAX_FILE_SIZE / 1024 / 1024), null); |
| | | } |
| | | |
| | | // ä¼ è¾æä»¶ |
| | | String oldName = file.getOriginalFilename(); |
| | | String filePath = pathHelper.getTempPath() + File.separator + oldName; |
| | | File newFile = new File(filePath); |
| | | file.transferTo(newFile); |
| | | double sizes = FileHelper.sizeToMb(file.getSize()); |
| | | |
| | | // è·åMD5 |
| | | String md5 = FileHelper.getFileMd5(filePath); |
| | | AttachEntity entity = attachService.selectByGuid(md5); |
| | | if (entity != null) { |
| | | newFile.delete(); |
| | | return ctrl.success("æä»¶å·²åå¨", md5); |
| | | } |
| | | |
| | | AttachEntity ae = getAttachEntity(ue, tab, oldName, md5, sizes); |
| | | String targetPath = pathHelper.getConfig().getUploadPath() + File.separator + ae.getPath(); |
| | | newFile.renameTo(new File(targetPath)); |
| | | |
| | | int rows = attachService.insert(ae); |
| | | |
| | | return rows > 0 ? ctrl.success(md5) : ctrl.fail("ä¿åæä»¶å¤±è´¥", null); |
| | | } catch (Exception ex) { |
| | | return ctrl.fail(ex.getMessage(), null); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·åéä»¶å®ä½ç±» |
| | | */ |
| | | protected AttachEntity getAttachEntity(UserEntity ue, String tab, String oldName, String md5, double sizes) { |
| | | AttachEntity entity = new AttachEntity(); |
| | | entity.setName(oldName); |
| | | entity.setTab(tab); |
| | | entity.setGuid(md5); |
| | | String subPath = PathHelper.getUploadPath() + File.separator + md5; |
| | | entity.setPath(subPath); |
| | | entity.setSizes(sizes); |
| | | if (ue != null) { |
| | | entity.setCreateUser(ue.getId()); |
| | | } |
| | | |
| | | return entity; |
| | | } |
| | | |
| | | /** |
| | | * ä¸è½½æä»¶ |
| | | */ |
| | | public void download(String guid, HttpServletResponse res) { |
| | | 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; |
| | | } |
| | | |
| | | String file = pathHelper.getConfig().getUploadPath() + File.separator + entity.getPath(); |
| | | WebHelper.download(file, entity.getName(), res); |
| | | } 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); |
| | | } |
| | | log.error(ex.getMessage(), ex); |
| | | } |
| | | } |
| | | } |