Skip to content

Commit

Permalink
[#4476]use more stable apis to create InputStream (#4478)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Aug 31, 2024
1 parent 27924f9 commit 0863a3b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class TestDownloadSchema implements CategorizedTestCase {
@Override
public void testRestTransport() throws Exception {
testDownloadFileAndDeleted();
testDownloadFileAndDeletedCN();
testDownloadFileNotDeleted();
testDownloadFileWithNull();
testSetContentTypeByResponseEntity();
Expand Down Expand Up @@ -97,6 +98,19 @@ private void testDownloadFileAndDeleted() throws Exception {
TestMgr.check(exists, false);
}

private void testDownloadFileAndDeletedCN() throws Exception {
RestOperations restTemplate = RestTemplateBuilder.create();
ReadStreamPart readStreamPart = restTemplate
.getForObject("servicecomb://springmvc/download/deleteAfterFinished?content={1}&fileName={2}",
ReadStreamPart.class, "hello", "中文");
String hello = readStreamPart.saveAsString().get();
TestMgr.check(hello, "hello");

boolean exists = restTemplate
.getForObject("servicecomb://springmvc/download/assertLastFileDeleted", boolean.class);
TestMgr.check(exists, false);
}

private void testDownloadFileWithNull() throws Exception {
RestOperations restTemplate = RestTemplateBuilder.create();
ReadStreamPart readStreamPart = restTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public void testRestTransport() throws Exception {
testUploadMultiBigFiles();
testFileUploadMultiRpc();
testUploadFileAndAttribute();
testUploadFileAndAttributeCN();
testUploadFileRequestPartAttribute();
testUploadFileRequestPartAttributeList();
}
Expand Down Expand Up @@ -127,6 +128,23 @@ private void testUploadFileAndAttribute() throws Exception {
TestMgr.check("hi test", result);
}

private void testUploadFileAndAttributeCN() throws Exception {
RestOperations template = RestTemplateBuilder.create();
Map<String, Object> map = new HashMap<>();
String message = "hi";
File file = File.createTempFile("中文名称", ".txt");
FileUtils.writeStringToFile(file, "test", StandardCharsets.UTF_8, false);

map.put("file", new FileSystemResource(file));
map.put("attribute", message);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(org.springframework.http.MediaType.MULTIPART_FORM_DATA);
String result = template.postForObject("servicecomb://springmvc/upload/uploadFileAndAttribute",
new HttpEntity<>(map, headers), String.class);
TestMgr.check("hi test", result);
}


private void testUploadFileRequestPartAttribute() throws Exception {
RestOperations template = RestTemplateBuilder.create();
Map<String, Object> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,15 @@ private File createTempFile(String name, String content) throws IOException {
}

@GetMapping(path = "/deleteAfterFinished")
public ResponseEntity<Part> deleteAfterFinished(@RequestParam("content") String content) throws IOException {
File file = createTempFile(content);
public ResponseEntity<Part> deleteAfterFinished(@RequestParam("content") String content,
@RequestParam(value = "fileName", required = false) String fileName) throws IOException {
File file;

if (StringUtils.isNotEmpty(fileName)) {
file = createTempFile(fileName, content);
} else {
file = createTempFile(content);
}

return ResponseEntity
.ok()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package org.apache.servicecomb.foundation.common.part;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import org.apache.commons.io.FileUtils;

Expand All @@ -41,7 +41,7 @@ public FilePart(String name, File file) {

@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(file);
return Files.newInputStream(file.toPath());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package org.apache.servicecomb.foundation.vertx.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import org.apache.commons.io.FileUtils;
import org.apache.servicecomb.foundation.common.part.AbstractPart;
Expand All @@ -36,7 +36,7 @@ public FileUploadPart(FileUpload fileUpload) {

@Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(fileUpload.uploadedFileName());
return Files.newInputStream(new File(fileUpload.uploadedFileName()).toPath());
}

@Override
Expand Down

0 comments on commit 0863a3b

Please sign in to comment.