|
|
@@ -0,0 +1,91 @@
|
|
|
+package com.uas.platform.b2b.service.impl;
|
|
|
+
|
|
|
+import java.io.BufferedInputStream;
|
|
|
+import java.io.BufferedOutputStream;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import com.uas.platform.b2b.core.BaseUtil;
|
|
|
+import com.uas.platform.b2b.dao.AttachDao;
|
|
|
+import com.uas.platform.b2b.model.Attach;
|
|
|
+import com.uas.platform.b2b.model.FileUpload;
|
|
|
+import com.uas.platform.b2b.service.AttachService;
|
|
|
+
|
|
|
+
|
|
|
+@Service
|
|
|
+public class AttachServiceImpl implements AttachService{
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private AttachDao attachDao;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Attach> getAttachById(Long id) {
|
|
|
+ return attachDao.findAttachByAtId(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long saveAttachPath(Attach attach) {
|
|
|
+ attach.setAtId(null);
|
|
|
+ return attachDao.save(attach).getAtId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadAttach(FileUpload uploadItem) {
|
|
|
+ String filename = uploadItem.getFile().getOriginalFilename();
|
|
|
+ long size = uploadItem.getFile().getSize();
|
|
|
+ if (size > 104857600) {
|
|
|
+ return "FORBIDEN";//文件过大,上传失败
|
|
|
+ }
|
|
|
+ String path = getFilePath(filename, "postattach", "bussinessCodeImg");
|
|
|
+ File file = new File(path);
|
|
|
+ BufferedOutputStream bos = null;
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ try {
|
|
|
+ bos = new BufferedOutputStream(new FileOutputStream(file));
|
|
|
+ bis = new BufferedInputStream(uploadItem.getFile().getInputStream());
|
|
|
+ int c;
|
|
|
+ while ((c = bis.read()) != -1) {
|
|
|
+ bos.write(c);
|
|
|
+ bos.flush();
|
|
|
+ }
|
|
|
+ bis.close();
|
|
|
+ bos.close();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return "FAILED";//操作失败
|
|
|
+ }
|
|
|
+ Attach attach = new Attach();
|
|
|
+ attach.setAtPath(path);
|
|
|
+ attach.setAtDescription("客户营业执照复印件或照片");
|
|
|
+ attach.setAtName(filename);
|
|
|
+ Long id = saveAttachPath(attach);
|
|
|
+ return id.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getFilePath(String fileName, String dirName, String userName) {
|
|
|
+ String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
|
|
|
+ String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length())
|
|
|
+ : "";
|
|
|
+ String path = BaseUtil.FILEPATH + dirName;
|
|
|
+ File file = new File(path);
|
|
|
+ if (!file.isDirectory()) {
|
|
|
+ file.mkdir();
|
|
|
+ path = path + File.separator + userName;
|
|
|
+ new File(path).mkdir();
|
|
|
+ } else {
|
|
|
+ path = path + File.separator + userName;
|
|
|
+ file = new File(path);
|
|
|
+ if (!file.isDirectory()) {
|
|
|
+ file.mkdir();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return path + File.separator + uuid + suffix;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|