Javascript 具有多个文件的JAXRS响应
我能够向ReactJS发送单个图像文件。但我必须向ReactJS发送多个图像文件 JAXRS代码:Javascript 具有多个文件的JAXRS响应,javascript,java,reactjs,rest,jersey-2.0,Javascript,Java,Reactjs,Rest,Jersey 2.0,我能够向ReactJS发送单个图像文件。但我必须向ReactJS发送多个图像文件 JAXRS代码: @GET @Path("/download/file1") @Produces( MediaType.APPLICATION_OCTET_STREAM ) public Response getFile() { String fileName = DOWNLOAD_FILE_SERVER+"BMI/testcases/B
@GET
@Path("/download/file1")
@Produces( MediaType.APPLICATION_OCTET_STREAM )
public Response getFile() {
String fileName = DOWNLOAD_FILE_SERVER+"BMI/testcases/Basispath_BMI_0_out.gif";
File file = new File( fileName);
System.out.println("fileName inside getFile = "+fileName);
final String filename = "testcase1.gif";
System.out.println("filename inside getFile = "+filename);
// Create the JAXRS response
// Don't forget to include the filename in 2 HTTP headers:
//
// a) The standard 'Content-Disposition' one, and
// b) The custom 'X-Suggested-Filename'
final Response.ResponseBuilder builder = Response.ok(
file, MediaType.APPLICATION_OCTET_STREAM)
.header("X-Actual-Content-Length", file.length())
.header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
// All Done.
return builder.build();
}
代码:
downloadImage = () => {
axios({
url: 'http://localhost:9900/EXAMPLE/rest/myresource/download/file1',
method: 'GET',
responseType: 'blob',
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
var filename = 'Image1.gif'
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
})
.catch((response) => { // then print response status
console.log("response = ",response);
})
}
问题:
也许只需对图像进行Base64编码并将其作为JSON字符串发送即可。你可以在客户端解码。谢谢你的回复。有没有示例代码?