Commit ec6ab2ed92bccacf1975cd43a46c3604925f3561

Authored by lijianfa_14810364212
Committed by xp.Huang
1 parent 64b15ef8

fix:文件上传返回路径双斜杆//替换成单斜杆/


(cherry picked from commit 2a633fe81c2a23cdff1bf6a1bca48319b0884144)
@@ -49,12 +49,12 @@ public class LocalFileStorageService implements FileStorageService { @@ -49,12 +49,12 @@ public class LocalFileStorageService implements FileStorageService {
49 public LocalFileStorageService(FileStorageProperties fileStorageProperties) { 49 public LocalFileStorageService(FileStorageProperties fileStorageProperties) {
50 this.fileStorageProperties = fileStorageProperties; 50 this.fileStorageProperties = fileStorageProperties;
51 this.fileStorageLocation = 51 this.fileStorageLocation =
52 - Paths.get(fileStorageProperties.getUploadDir()).toAbsolutePath().normalize(); 52 + Paths.get(fileStorageProperties.getUploadDir()).toAbsolutePath().normalize();
53 try { 53 try {
54 Files.createDirectories(this.fileStorageLocation); 54 Files.createDirectories(this.fileStorageLocation);
55 } catch (Exception e) { 55 } catch (Exception e) {
56 throw new RuntimeException( 56 throw new RuntimeException(
57 - "Could not create the directory where the uploaded files will be stored."); 57 + "Could not create the directory where the uploaded files will be stored.");
58 } 58 }
59 } 59 }
60 60
@@ -86,19 +86,18 @@ public class LocalFileStorageService implements FileStorageService { @@ -86,19 +86,18 @@ public class LocalFileStorageService implements FileStorageService {
86 @Override 86 @Override
87 public FileUploadResponse upload(MultipartFile file) { 87 public FileUploadResponse upload(MultipartFile file) {
88 String fileName = this.storeFile(file); 88 String fileName = this.storeFile(file);
89 -// String fileDownloadPath = ServletUriComponentsBuilder.fromCurrentRequestUri() .path(fileStorageProperties.getDownloadPath()) .path(fileName) .toUriString();  
90 - String fileDownloadPath = fileStorageProperties.getDownloadPath()+"/"+fileName;  
91 - String realDownloadPath = fileDownloadPath.replace(fileStorageProperties.getUploadPath(), ""); 89 + String fileDownloadPath = fileStorageProperties.getDownloadPath() + "/" + fileName;
  90 + String realDownloadPath = fileDownloadPath.replace(fileStorageProperties.getUploadPath(), "").replace("//", "/").replace("\\","/");
92 91
93 String ossStaticPath = ServletUriComponentsBuilder.fromCurrentContextPath().toUriString() + ossFileUrl; 92 String ossStaticPath = ServletUriComponentsBuilder.fromCurrentContextPath().toUriString() + ossFileUrl;
94 - ossStaticPath = ossStaticPath.replace("**", fileName); 93 + ossStaticPath = ossStaticPath.replace("**", fileName).replace("//", "/").replace("\\","/");
95 return new FileUploadResponse( 94 return new FileUploadResponse(
96 - fileName, ossStaticPath, file.getContentType(), file.getSize(), realDownloadPath); 95 + fileName, ossStaticPath, file.getContentType(), file.getSize(), realDownloadPath);
97 } 96 }
98 97
99 @Override 98 @Override
100 public ResponseEntity<?> download( 99 public ResponseEntity<?> download(
101 - String fileName, HttpServletRequest request, HttpServletResponse response) { 100 + String fileName, HttpServletRequest request, HttpServletResponse response) {
102 Resource resource = this.loadFileAsResource(fileName); 101 Resource resource = this.loadFileAsResource(fileName);
103 String contentType = null; 102 String contentType = null;
104 try { 103 try {
@@ -113,35 +112,35 @@ public class LocalFileStorageService implements FileStorageService { @@ -113,35 +112,35 @@ public class LocalFileStorageService implements FileStorageService {
113 } 112 }
114 113
115 return ResponseEntity.ok() 114 return ResponseEntity.ok()
116 - .contentType(MediaType.parseMediaType(contentType))  
117 - .header(  
118 - HttpHeaders.CONTENT_DISPOSITION,  
119 - "attachment; filename=\"" + resource.getFilename() + "\"")  
120 - .body(resource); 115 + .contentType(MediaType.parseMediaType(contentType))
  116 + .header(
  117 + HttpHeaders.CONTENT_DISPOSITION,
  118 + "attachment; filename=\"" + resource.getFilename() + "\"")
  119 + .body(resource);
121 } 120 }
122 121
123 @Override 122 @Override
124 public InputStream download(String file) throws IOException { 123 public InputStream download(String file) throws IOException {
125 - String fileName=file.substring(file.lastIndexOf("/")+1);  
126 - String filePath=fileStorageProperties.getUploadDir()+File.separator+ fileName;  
127 - return new FileInputStream(new File(filePath)); 124 + String fileName = file.substring(file.lastIndexOf("/") + 1);
  125 + String filePath = fileStorageProperties.getUploadDir() + File.separator + fileName;
  126 + return new FileInputStream(new File(filePath));
128 } 127 }
129 128
130 @Override 129 @Override
131 - public String uploadFile(String name, String contentType, InputStream inputStream )  
132 - throws IOException { 130 + public String uploadFile(String name, String contentType, InputStream inputStream)
  131 + throws IOException {
133 String fileName = storeFileByInputStream(name, inputStream); 132 String fileName = storeFileByInputStream(name, inputStream);
134 - String fileDownloadPath = fileStorageProperties.getDownloadPath()+"/"+fileName;  
135 - String realDownloadPath = fileDownloadPath.replace(fileStorageProperties.getUploadPath(), ""); 133 + String fileDownloadPath = fileStorageProperties.getDownloadPath() + "/" + fileName;
  134 + String realDownloadPath = fileDownloadPath.replace(fileStorageProperties.getUploadPath(), "").replace("//", "/").replace("\\","/");
136 return realDownloadPath; 135 return realDownloadPath;
137 } 136 }
138 137
139 @Override 138 @Override
140 public boolean deleteFile(String deleteFilePath) { 139 public boolean deleteFile(String deleteFilePath) {
141 - String fileName=deleteFilePath.substring(deleteFilePath.lastIndexOf("/")+1);  
142 - String filePath=fileStorageProperties.getUploadDir()+File.separator+ fileName;  
143 - File file=new File(filePath);  
144 - if(file.exists()){ 140 + String fileName = deleteFilePath.substring(deleteFilePath.lastIndexOf("/") + 1);
  141 + String filePath = fileStorageProperties.getUploadDir() + File.separator + fileName;
  142 + File file = new File(filePath);
  143 + if (file.exists()) {
145 file.delete(); 144 file.delete();
146 return true; 145 return true;
147 } 146 }
@@ -150,12 +149,12 @@ public class LocalFileStorageService implements FileStorageService { @@ -150,12 +149,12 @@ public class LocalFileStorageService implements FileStorageService {
150 149
151 150
152 private String storeFileByInputStream(String fileName, InputStream inputStream) 151 private String storeFileByInputStream(String fileName, InputStream inputStream)
153 - throws IOException { 152 + throws IOException {
154 // random fileName if needed 153 // random fileName if needed
155 if (fileStorageProperties.isRandomFileName()) { 154 if (fileStorageProperties.isRandomFileName()) {
156 if (fileName.contains(".")) { 155 if (fileName.contains(".")) {
157 fileName = 156 fileName =
158 - RandomStringUtils.randomAlphabetic(15) + fileName.substring(fileName.lastIndexOf(".")); 157 + RandomStringUtils.randomAlphabetic(15) + fileName.substring(fileName.lastIndexOf("."));
159 } else { 158 } else {
160 fileName = RandomStringUtils.randomAlphabetic(15); 159 fileName = RandomStringUtils.randomAlphabetic(15);
161 } 160 }
@@ -173,4 +172,9 @@ public class LocalFileStorageService implements FileStorageService { @@ -173,4 +172,9 @@ public class LocalFileStorageService implements FileStorageService {
173 172
174 return fileName; 173 return fileName;
175 } 174 }
176 -} 175 +
  176 + public static void main(String[] args) {
  177 + String s = "a/b//c//d\\eec";
  178 + System.err.println(s.replace("//", "/").replace("\\","/"));
  179 + }
  180 +}