|
|
@@ -4,16 +4,24 @@ import java.io.BufferedInputStream;
|
|
|
import java.io.BufferedOutputStream;
|
|
|
import java.io.File;
|
|
|
import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
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;
|
|
|
+import com.uas.platform.b2b.support.SystemSession;
|
|
|
+import com.uas.platform.core.util.FileUtils;
|
|
|
|
|
|
@Service
|
|
|
public class AttachServiceImpl implements AttachService {
|
|
|
@@ -43,7 +51,7 @@ public class AttachServiceImpl implements AttachService {
|
|
|
if (size > 104857600) {
|
|
|
return null;// 文件过大,上传失败
|
|
|
}
|
|
|
- String path = getFilePath(filename, parentDir);
|
|
|
+ String path = randomFileName(filename, parentDir);
|
|
|
File file = new File(path);
|
|
|
BufferedOutputStream bos = null;
|
|
|
BufferedInputStream bis = null;
|
|
|
@@ -63,23 +71,76 @@ public class AttachServiceImpl implements AttachService {
|
|
|
return new Attach(filename, path, description, size);
|
|
|
}
|
|
|
|
|
|
- private String getFilePath(String fileName, String parentDir) {
|
|
|
+ public String randomFileName(String fileName, String parentDir) {
|
|
|
String uuid = UUID.randomUUID().toString().replaceAll("\\-", "");
|
|
|
- String suffix = fileName.indexOf(".") != -1 ? fileName.substring(fileName.lastIndexOf("."), fileName.length()) : "";
|
|
|
- String path = BaseUtil.FILEPATH + "postattach";
|
|
|
- File file = new File(path);
|
|
|
- if (!file.isDirectory()) {
|
|
|
- file.mkdir();
|
|
|
- path = path + File.separator + parentDir;
|
|
|
- new File(path).mkdir();
|
|
|
- } else {
|
|
|
- path = path + File.separator + parentDir;
|
|
|
- file = new File(path);
|
|
|
- if (!file.isDirectory()) {
|
|
|
- file.mkdir();
|
|
|
+ String suffix = (fileName != null && fileName.indexOf(".") != -1) ? fileName
|
|
|
+ .substring(fileName.lastIndexOf("."), fileName.length()) : "";
|
|
|
+ return getDir(parentDir) + File.separator + uuid + suffix;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<Attach> uploadZipAndSave(FileUpload uploadItem, String parentDir, String description,
|
|
|
+ Map<String, Map<String, Object>> entryParams) {
|
|
|
+ Map<String, String> fileMap = uploadZip(uploadItem, parentDir, description);
|
|
|
+ if (fileMap != null) {
|
|
|
+ List<Attach> attachs = new ArrayList<Attach>();
|
|
|
+ for (String entry : fileMap.keySet()) {
|
|
|
+ if (entryParams.containsKey(entry)) {
|
|
|
+ File file = new File(fileMap.get(entry));
|
|
|
+ if (file.exists()) {
|
|
|
+ Map<String, Object> params = entryParams.get(entry);
|
|
|
+ Attach attach = new Attach(String.valueOf(params.get("name")), fileMap.get(entry), description, file.length());
|
|
|
+ attach = attachDao.save(attach);
|
|
|
+ attach.setSourceId(Long.parseLong(entryParams.get("sourceId").toString()));
|
|
|
+ attachs.add(attach);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return attachs;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Map<String, String> uploadZip(FileUpload uploadItem, String parentDir, String description) {
|
|
|
+ String unzipDir = getDir(parentDir);
|
|
|
+ try {
|
|
|
+ List<String> entries = FileUtils.unzip(uploadItem.getFile().getInputStream(), unzipDir);
|
|
|
+ if (!CollectionUtils.isEmpty(entries)) {
|
|
|
+ Map<String, String> fileMap = new HashMap<String, String>();
|
|
|
+ for (String entry : entries) {
|
|
|
+ fileMap.put(entry, unzipDir + File.separator + entry);
|
|
|
+ }
|
|
|
+ return fileMap;
|
|
|
}
|
|
|
+ } catch (IOException e) {
|
|
|
+
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 文件路径
|
|
|
+ *
|
|
|
+ * @param parentDir
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getDir(String parentDir) {
|
|
|
+ String path = BaseUtil.FILEPATH + "postattach";
|
|
|
+ File dir = new File(path);
|
|
|
+ if (!dir.isDirectory())
|
|
|
+ dir.mkdir();
|
|
|
+ path += File.separator + parentDir;
|
|
|
+ dir = new File(path);
|
|
|
+ if (!dir.isDirectory())
|
|
|
+ dir.mkdir();
|
|
|
+ if (SystemSession.getUser() != null) {
|
|
|
+ path += File.separator + SystemSession.getUser().getEnterprise();
|
|
|
+ dir = new File(path);
|
|
|
+ if (!dir.isDirectory())
|
|
|
+ dir.mkdir();
|
|
|
}
|
|
|
- return path + File.separator + uuid + suffix;
|
|
|
+ return path;
|
|
|
}
|
|
|
|
|
|
}
|