Skip to content

Commit

Permalink
[#4476]use more stable apis to create InputStream (#4484)
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 authored Aug 31, 2024
1 parent f3dc998 commit 6ccef9d
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

import com.netflix.config.DynamicPropertyFactory;
Expand All @@ -39,6 +40,7 @@ public class TestDownloadSchema implements CategorizedTestCase {
@Override
public void testRestTransport() throws Exception {
testDownloadFileAndDeleted();
testDownloadFileAndDeletedCN();
testDownloadFileNotDeleted();
testDownloadFileWithNull();
testSetContentTypeByResponseEntity();
Expand Down Expand Up @@ -106,6 +108,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 {
RestTemplate restTemplate = RestTemplateBuilder.create();
ReadStreamPart readStreamPart = restTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;

@Component
Expand All @@ -59,6 +60,7 @@ public void testRestTransport() throws Exception {
testUploadMultiBigFiles();
testFileUploadMultiRpc();
testUploadFileAndAttribute();
testUploadFileAndAttributeCN();
testUploadFileRequestPartAttribute();
}

Expand Down Expand Up @@ -126,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 {
RestTemplate 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 6ccef9d

Please sign in to comment.