Explorar el Código

项目文件报错处理

user5 hace 4 años
padre
commit
86a8790bcb

+ 3 - 2
src/main/java/com/jeeplus/common/bos/BosController.java

@@ -14,6 +14,7 @@ import java.io.*;
 import java.util.Calendar;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.UUID;
 
 @Controller
 @RequestMapping(value = "${adminPath}/bos/")
@@ -30,7 +31,7 @@ public class BosController {
             try {
                 //MultipartFile转File
                 File srcFile = transformMultipartFile(file);
-                String path = directory+"/"+storeAs+datePath()+"/"+srcFile.getName();
+                String path = directory+"/"+storeAs+datePath()+"/"+ UUID.randomUUID().toString().replaceAll("-","")+"/"+srcFile.getName();
                 BOSClientUtil bosClientUtil = new BOSClientUtil();
                 InputStream inputStream = file.getInputStream();
                 String upload = bosClientUtil.upload(path, inputStream);
@@ -51,7 +52,7 @@ public class BosController {
                 String uploadFile = fileUploadUtil.uploadFile(uploadPath, srcFile, srcFile.getName());
                 map.put("msg","上传成功");
                 map.put("code","1");
-                map.put("url",uploadFile);
+                map.put("url","/a/workfullmanage/workFullManage/downLoadAttach?file=" + uploadFile);
             } catch (Exception e) {
                 e.printStackTrace();
                 map.put("msg","上传失败");

+ 1 - 1
src/main/java/com/jeeplus/modules/projectAccessory/service/ProjectAccessoryService.java

@@ -246,7 +246,7 @@ public class ProjectAccessoryService extends TreeService<ProjectAccessoryDao, Pr
         WorkClientAttachment attchment = new WorkClientAttachment();
         attchment.setAttachmentId(attachmentId);
         attchment.setProjectId(projectId);
-        List<WorkClientAttachment> attachments = workattachmentService.getAttachmentList(attchment);
+        List<WorkClientAttachment> attachments = workattachmentService.getAttachmentListOnProjectAccessory(attchment);
         return attachments;
     }
 

+ 12 - 0
src/main/java/com/jeeplus/modules/sys/service/WorkattachmentService.java

@@ -279,6 +279,18 @@ public class WorkattachmentService extends CrudService<WorkattachmentDao, Workat
 	}
 
 	/**
+	 * 项目获取附件信息
+	 * @param workClientAttachment
+	 * @return
+	 */
+	public List<WorkClientAttachment> getAttachmentListOnProjectAccessory(WorkClientAttachment workClientAttachment){
+		List<WorkClientAttachment> list = workClientAttachmentDao.getList(workClientAttachment);
+		//数据处理
+		workClientAttachmentManage(list);
+		return list;
+	}
+
+	/**
 	 * 获取附件信息
 	 * @param workClientAttachment
 	 * @return

+ 21 - 1
src/main/java/com/jeeplus/modules/sys/web/WorkattachmentController.java

@@ -16,6 +16,7 @@ import com.jeeplus.common.web.BaseController;
 import com.jeeplus.modules.sys.entity.Workattachment;
 import com.jeeplus.modules.sys.service.WorkattachmentService;
 import com.jeeplus.modules.sys.utils.UserUtils;
+import com.jeeplus.modules.utils.SftpClientUtil;
 import org.apache.shiro.authz.annotation.Logical;
 import org.apache.shiro.authz.annotation.RequiresPermissions;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -43,6 +44,8 @@ import java.util.Map;
 @Controller
 @RequestMapping(value = "${adminPath}/sys/workattachment")
 public class WorkattachmentController extends BaseController {
+	/** 文件存储方式(0:本地服务器存储。1:云端存储) */
+	private final static String uploadMode = Global.getConfig("remoteServer.uploadMode");
 
 	@Autowired
 	private WorkattachmentService workattachmentService;
@@ -211,7 +214,24 @@ public class WorkattachmentController extends BaseController {
 	public HashMap<Object, Object> deleteFileFromAliyun(String id,String type,String url,Model model){
 		if(StringUtils.isNotBlank(id)) {
 			Workattachment workattachment = workattachmentService.get(id);
-			workattachmentService.deleteFileFromAliyun(workattachment, workattachment == null ? url : workattachment.getUrl());
+			if("1".equals(uploadMode)) {
+				workattachmentService.deleteFileFromAliyun(workattachment, workattachment == null ? url : workattachment.getUrl());
+			}else{
+				SftpClientUtil sftpClientUtil=new SftpClientUtil();
+				try {
+					if(null != workattachment){
+						sftpClientUtil.delete(workattachment.getUrl());
+					}else{
+						sftpClientUtil.delete(url);
+					}
+
+					if(workattachment != null){
+						workattachmentService.delete(workattachment);
+					}
+				} catch (Exception e) {
+					e.printStackTrace();
+				}
+			}
 		}
 		HashMap<Object, Object> map = Maps.newHashMap();
 		map.put("str","success");

+ 20 - 10
src/main/java/com/jeeplus/modules/utils/SftpClientUtil.java

@@ -63,7 +63,7 @@ public class SftpClientUtil {
             if (!dir.exists()) {
                 dir.mkdirs();
             }
-            String filePath = path +"/" + file.getName();
+            String filePath = path +"/" + UUID.randomUUID().toString().replaceAll("-", "") + file.getName();
             //File转MultipartFile文件
             MultipartFile multipartFile = transformFile(file);
             //保存文件
@@ -89,10 +89,11 @@ public class SftpClientUtil {
             sftp.cd(remoteFolder);
         }catch (SftpException e){
         }
-        sftp.put(fileStream, remoteFileName);
+        String remoteName = UUID.randomUUID().toString().replaceAll("-", "") + remoteFileName;
+        sftp.put(fileStream, remoteName);
         disconnect(sftp);
         logger.info("文件上传成功!! 耗时:{"+(System.currentTimeMillis() - start)+"}ms");
-        return remoteFolder+"/"+remoteFileName;
+        return remoteFolder+"/"+remoteName;
     }
 
     /**
@@ -103,10 +104,19 @@ public class SftpClientUtil {
      * @throws Exception
      */
     public void delete(String deleteFile) throws Exception {
-        ChannelSftp sftp = connect();
-        sftp.cd(directory);
-        sftp.rm(deleteFile);
-        disconnect(sftp);
+        if(StringUtils.isNotBlank(host) && ("127.0.0.1".equals(host) || "localhost".equals(host))) {
+            //根据路径创建文件对象
+            File file = new File(deleteFile);
+            //路径是个文件且不为空时删除文件
+            if(file.isFile()&&file.exists()){
+                file.delete();
+            }
+        }else{
+            ChannelSftp sftp = connect();
+            sftp.cd(directory);
+            sftp.rm(deleteFile);
+            disconnect(sftp);
+        }
     }
 
     /**
@@ -243,8 +253,8 @@ public class SftpClientUtil {
                 File fileurl = new File(wPath);
                 //将文件读入文件流
                 InputStream inStream = new FileInputStream(fileurl);
-                //判断浏览器代理并分别设置响应给浏览器的编码格式
-                String finalFileName = URLEncoder.encode(downloadFile,"UTF8");
+                //判断浏览器代理并分别设置响应给浏览器的编码格式(并去除文件服务器中文件的UUID头展示信息)
+                String finalFileName = URLEncoder.encode(downloadFile.substring(32, downloadFile.length()),"UTF8");
                 //设置HTTP响应头
                 response.reset();//重置 响应头
                 response.setContentType("application/x-download");//告知浏览器下载文件,而不是直接打开,浏览器默认为打开
@@ -263,7 +273,7 @@ public class SftpClientUtil {
                 if(path != null && !"".equals(path)){
                     sftp.cd(path);
                 }
-                String fileName = URLEncoder.encode(downloadFile,"UTF-8");
+                String fileName = URLEncoder.encode(downloadFile.substring(32, downloadFile.length()),"UTF-8");
                 OutputStream out = null;
                 InputStream in = sftp.get(downloadFile);
                 response.reset();//重置 响应头

+ 39 - 25
src/main/java/com/jeeplus/modules/workfullmanage/web/WorkFullManageController.java

@@ -21,6 +21,7 @@ import com.jeeplus.modules.sys.service.SystemService;
 import com.jeeplus.modules.sys.utils.DictUtils;
 import com.jeeplus.modules.sys.utils.UserUtils;
 import com.jeeplus.modules.tools.utils.TwoDimensionCode;
+import com.jeeplus.modules.utils.SftpClientUtil;
 import com.jeeplus.modules.workclientinfo.entity.WorkClientAttachment;
 import com.jeeplus.modules.workclientinfo.entity.WorkClientInfo;
 import com.jeeplus.modules.workclientinfo.entity.WorkClientLinkman;
@@ -70,6 +71,9 @@ import freemarker.template.Template;
 @RequestMapping(value = "${adminPath}/workfullmanage/workFullManage")
 public class WorkFullManageController extends BaseController {
 
+	/** 文件存储方式(0:本地服务器存储。1:云端存储) */
+	private final static String uploadMode = Global.getConfig("remoteServer.uploadMode");
+
 	@Autowired
 	private WorkFullManageService workFullManageService;
 
@@ -583,35 +587,45 @@ public class WorkFullManageController extends BaseController {
 //		return null;
 		OutputStream out = null;
 		InputStream in = null;
-		try {
-			file = URLDecoder.decode(file,"UTF-8");
-			String path = file.substring(file.indexOf(".com") + 4, file.length());
-			String fileName = URLEncoder.encode(file.substring(file.lastIndexOf("/") + 1, file.length()),"UTF-8");
-			BOSClientUtil bosClientUtil = new BOSClientUtil();
-			in = bosClientUtil.getObject(path);
-			response.reset();//重置 响应头
-			response.setContentType("application/x-download");
-			response.setHeader("Content-disposition", "attachment; filename=" + fileName);
-			out = response.getOutputStream();
-			byte[] b = new byte[1024];
-			int len;
-			while ((len = in.read(b)) > 0){
-				response.getOutputStream().write(b, 0, len);
-			}
-			in.close();
-			out.close();
-		}catch (IOException e){
-			e.printStackTrace();
-		}finally {
-			if (out != null) {
-				out.close();
-			}
-			if (in != null) {
+		if("1".equals(uploadMode)){
+			try {
+				file = URLDecoder.decode(file,"UTF-8");
+				String path = file.substring(file.indexOf(".com") + 4, file.length());
+				String fileName = URLEncoder.encode(file.substring(file.lastIndexOf("/") + 1, file.length()),"UTF-8");
+				BOSClientUtil bosClientUtil = new BOSClientUtil();
+				in = bosClientUtil.getObject(path);
+				response.reset();//重置 响应头
+				response.setContentType("application/x-download");
+				response.setHeader("Content-disposition", "attachment; filename=" + fileName);
+				out = response.getOutputStream();
+				byte[] b = new byte[1024];
+				int len;
+				while ((len = in.read(b)) > 0){
+					response.getOutputStream().write(b, 0, len);
+				}
 				in.close();
+				out.close();
+			}catch (IOException e){
+				e.printStackTrace();
+			}finally {
+				if (out != null) {
+					out.close();
+				}
+				if (in != null) {
+					in.close();
+				}
 			}
+		}else{
+			file = URLDecoder.decode(file,"UTF-8");
+			String fileName = file.substring(file.lastIndexOf("/") + 1, file.length());
+			file = file.substring(0,file.lastIndexOf("/"));
+			file = URLDecoder.decode(file,"UTF-8");
+
+			SftpClientUtil sftpClientUtil=new SftpClientUtil();
+			sftpClientUtil.download(file,fileName,response);
 		}
-      return  null;
 
+		return null;
 
 	}