gpt4 book ai didi

Spring : get response as Multipart File from REST WebService

转载 作者:行者123 更新时间:2023-12-03 18:17:17 25 4
gpt4 key购买 nike

我正在使用 Spring 4.0 为 RESTFUL Web 服务创建 POC。 要求是从 REST WEB-Service 接收 MultipartFile 作为响应。

REST 服务 Controller

@RequestMapping(value="/getcontent/file", method=RequestMapping.post)
public MultipartFile getMultipartAsFileAsObject() {

File file = new File("src/test/resources/input.docx");
FileInputStream input = new FileInputStream(file);
MultipartFile multipartFile = new MockMultipartFile("file",file.getName(),
"application/docx", IOUtils.toByteArray(input));

return multipartFile
}

我也使用第三方客户端和 Apache Http 客户端调用此服务。请看看输出。

使用第三方 REST 客户端,即。 postman

输出看起来像 Json -
{
"name" : "file",
"originalfilename" : "sample.docx",
"contentType" : "application/docx",
"content" : [
82,
101,
97,
100,
101,
32,
32,
.
.
.
.
.
]
}

Apache HTTP 客户端示例代码
private static void executeClient() {
HttpClient client = new DefaultHttpClient();
HttpPost postReqeust = new HttpPost(SERVER_URI);

try{
// Set Various Attributes

HttpResponse response = client.execute(postReqeust) ;

//Verify response if any
if (response != null)
{
InputStream inputStream = response.getEntity().getContent();
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);

OutputStream outputStream = new FileOutputStream
(new File("src/main/resource/sample.docx"));
outputStream.write(buffer);
outputStream.flush();
outputStream.close();
}

}
catch(Exception ex){
ex.printStackTrace();
}

Apache Http 客户端的输出

文件正在创建,但它是空的。 (0 字节)。

最佳答案

 I found some interesting answers from multiple stackoverflow questions.  Links are given below

file downloading in restful web services

what's the correct way to send a file from REST web service to client?

For Sending single file : (copied from above sources)

@GET
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile() {
File file = ... // Initialize this to the File path you want to serve.
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM)
.header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ) //optional
.build();
}

用于发送 Zip 文件:(从上述来源复制)

1)方法第一:

您可以使用上述方法发送任何文件/Zip。
private static final String FILE_PATH = "d:\\Test2.zip";
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile() {
File file = new File(FILE_PATH);
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename=newfile.zip");
return response.build();

}

2)方法二:
@GET
@Path("/get")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public StreamingOutput helloWorldZip() throws Exception {
return new StreamingOutput(){
@Override
public void write(OutputStream arg0) throws IOException, WebApplicationException {
// TODO Auto-generated method stub
BufferedOutputStream bus = new BufferedOutputStream(arg0);
try {
Thread.currentThread().getContextClassLoader().getResource("");
File file = new File("d:\\Test1.zip");
FileInputStream fizip = new FileInputStream(file);
byte[] buffer2 = IOUtils.toByteArray(fizip);
bus.write(buffer2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}

关于 Spring : get response as Multipart File from REST WebService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35030479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com