Commit a9dc7779e1a546ad27a077ea7a9b790bcfa4e8f9
Merge branch 'cherry-pick-2a633fe8' into 'master_dev'
fix:文件上传返回路径双斜杆//替换成单斜杆/ See merge request yunteng/thingskit!467
Showing
1 changed file
with
31 additions
and
27 deletions
... | ... | @@ -49,12 +49,12 @@ public class LocalFileStorageService implements FileStorageService { |
49 | 49 | public LocalFileStorageService(FileStorageProperties fileStorageProperties) { |
50 | 50 | this.fileStorageProperties = fileStorageProperties; |
51 | 51 | this.fileStorageLocation = |
52 | - Paths.get(fileStorageProperties.getUploadDir()).toAbsolutePath().normalize(); | |
52 | + Paths.get(fileStorageProperties.getUploadDir()).toAbsolutePath().normalize(); | |
53 | 53 | try { |
54 | 54 | Files.createDirectories(this.fileStorageLocation); |
55 | 55 | } catch (Exception e) { |
56 | 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 | 86 | @Override |
87 | 87 | public FileUploadResponse upload(MultipartFile file) { |
88 | 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 | 92 | String ossStaticPath = ServletUriComponentsBuilder.fromCurrentContextPath().toUriString() + ossFileUrl; |
94 | - ossStaticPath = ossStaticPath.replace("**", fileName); | |
93 | + ossStaticPath = ossStaticPath.replace("**", fileName).replace("//", "/").replace("\\","/"); | |
95 | 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 | 98 | @Override |
100 | 99 | public ResponseEntity<?> download( |
101 | - String fileName, HttpServletRequest request, HttpServletResponse response) { | |
100 | + String fileName, HttpServletRequest request, HttpServletResponse response) { | |
102 | 101 | Resource resource = this.loadFileAsResource(fileName); |
103 | 102 | String contentType = null; |
104 | 103 | try { |
... | ... | @@ -113,35 +112,35 @@ public class LocalFileStorageService implements FileStorageService { |
113 | 112 | } |
114 | 113 | |
115 | 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 | 122 | @Override |
124 | 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 | 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 | 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 | 135 | return realDownloadPath; |
137 | 136 | } |
138 | 137 | |
139 | 138 | @Override |
140 | 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 | 144 | file.delete(); |
146 | 145 | return true; |
147 | 146 | } |
... | ... | @@ -150,12 +149,12 @@ public class LocalFileStorageService implements FileStorageService { |
150 | 149 | |
151 | 150 | |
152 | 151 | private String storeFileByInputStream(String fileName, InputStream inputStream) |
153 | - throws IOException { | |
152 | + throws IOException { | |
154 | 153 | // random fileName if needed |
155 | 154 | if (fileStorageProperties.isRandomFileName()) { |
156 | 155 | if (fileName.contains(".")) { |
157 | 156 | fileName = |
158 | - RandomStringUtils.randomAlphabetic(15) + fileName.substring(fileName.lastIndexOf(".")); | |
157 | + RandomStringUtils.randomAlphabetic(15) + fileName.substring(fileName.lastIndexOf(".")); | |
159 | 158 | } else { |
160 | 159 | fileName = RandomStringUtils.randomAlphabetic(15); |
161 | 160 | } |
... | ... | @@ -173,4 +172,9 @@ public class LocalFileStorageService implements FileStorageService { |
173 | 172 | |
174 | 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 | +} | |
\ No newline at end of file | ... | ... |