Java 通过REST发送/接收图像
我正在使用grizzly for java rest服务,并在android应用程序中使用这些web服务 就“文本”数据而言,其工作良好 现在我想在我的android应用程序中加载图像(从服务器),使用这个rest服务,并允许用户从设备更新图像 我试过这个密码Java 通过REST发送/接收图像,java,android,web-services,jersey,grizzly,Java,Android,Web Services,Jersey,Grizzly,我正在使用grizzly for java rest服务,并在android应用程序中使用这些web服务 就“文本”数据而言,其工作良好 现在我想在我的android应用程序中加载图像(从服务器),使用这个rest服务,并允许用户从设备更新图像 我试过这个密码 @GET @Path("/img3") @Produces(MediaType.APPLICATION_OCTET_STREAM) public Response getFile() { File file = new File(
@GET
@Path("/img3")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile()
{
File file = new File("img/3.jpg");
return Response.ok(file, MediaType.APPLICATION_OCTET_STREAM).header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"") // optional
.build();
}
上面的代码允许我下载文件,但是否可以在broswer中显示结果?这样地
第1部分的解决方案: 我已经按照建议对代码进行了更改 请求的图像将显示在浏览器中 第二部分: 用于转换回Base64编码图像的代码
@POST
@Path("/upload/{primaryKey}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces("image/jpg")
public String uploadImage(@FormParam("image") String image, @PathParam("primaryKey") String primaryKey) throws SQLException, FileNotFoundException
{
String result = "false";
FileOutputStream fos;
fos = new FileOutputStream("img/" + primaryKey + ".jpg");
// decode Base64 String to image
try
{
byte byteArray[] = Base64.getMimeDecoder().decode(image);
fos.write(byteArray);
result = "true";
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}
但是否可以在broswer中显示结果?
。我想你已经试过了。所以请报告我很抱歉我不明白我应该报告什么。我仍在寻找一种在Browser中显示结果的解决方案。内容配置应该是内联的,并且媒体类型应该是正确的jpeg mime类型,而不是通用的八位字节流。@Shadow成功了。多谢各位much@Shadow我该怎么处理我的问题,它被搁置了?
@POST
@Path("/upload/{primaryKey}")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces("image/jpg")
public String uploadImage(@FormParam("image") String image, @PathParam("primaryKey") String primaryKey) throws SQLException, FileNotFoundException
{
String result = "false";
FileOutputStream fos;
fos = new FileOutputStream("img/" + primaryKey + ".jpg");
// decode Base64 String to image
try
{
byte byteArray[] = Base64.getMimeDecoder().decode(image);
fos.write(byteArray);
result = "true";
fos.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return result;
}