0
什么是使REST Web服务使用JAVA jax-rs发送多个图像的最佳方式?通过REST Web服务以base64字符串发送多个图像
什么是使REST Web服务使用JAVA jax-rs发送多个图像的最佳方式?通过REST Web服务以base64字符串发送多个图像
虽然使用base64字符串是一个选项,但有更好的方式去发送JAX-RS Web服务中的文件。我个人建议创建一个方法来侦听用户POST HTTP表单。这种形式可以真正包含您需要发送的所有信息以及标识符。
这与我将用于此类型的Web服务的方法定义类似。它与Jersey兼容,它允许用户通过基本的HTML表单或任何POST请求发送文件。我强烈建议看看构建Java Web服务的this article。它不包括“文件上传”方面,但它非常有用,它帮助我了解更多关于Java Web服务功能的更多信息。
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces("text/html")
public Response uploadFile(
@FormDataParam("username") String username,
@FormDataParam("password") String password,
@FormDataParam("title") String title,
@FormDataParam("file") InputStream fileInputString,
@FormDataParam("file") FormDataContentDisposition fileInputDetails) {
String status = null;
String fileLocation = "/home/user/uploadtest/test.png;
NumberFormat myFormat = NumberFormat.getInstance();
myFormat.setGroupingUsed(true);
// Save the file
try {
OutputStream out = new FileOutputStream(new File(fileLocation));
byte[] buffer = new byte[1024];
int bytes = 0;
long file_size = 0;
while ((bytes = fileInputString.read(buffer)) != -1) {
out.write(buffer, 0, bytes);
file_size += bytes;
}
out.flush();
out.close();
status = "File has been uploaded to:" + fileLocation;
} catch (IOException ex) {
System.err.println("Unable to save file: " + fileLocation);
ex.printStackTrace();
}
return Response.status(200).entity(status).build();
}
将它们作为json数组发送,此处每个数组元素将包含一个图像的base64编码字符串。 –
否。向图像发送一个URL数组。 – erickson