
正文
【Jersey】图片上传及显示
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
一、前期准备
图片上传需要用到的一些依赖:
<dependency>
<groupId>org.jvnet.mimepull</groupId>
<artifactId>mimepull</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-multipart</artifactId>
<version>${jersey.version}</version>
</dependency>
二、Jersey注解
@Path("/img")
表示访问路径为/img,并且可以接收参数,例如@Path("/images/{name}.{type}"),再利用@PathParam来接收name和type两个参数;同时也支持正则表达式,例如@Path("username/{username:[a-zA-Z][0-9]*}")
@POST
代表接受的HTTP请求类型为POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
表示接受的数据类型为"multipart/form-data"
@Produces(MediaType.APPLICATION_JSON)
表示发生出去的数据类型为"application/json"
@FormDataParam
接收图片、文件等特定类型数据
@Context
接收POST请求中发送的参数
@QueryParam
获取GET请求中url中的参数,例如请求url为http://localhost:8080/user?username=Amy&age=12&gender=male,那么可以使用:
@Path("/user")
@GET
public addUser(@QueryParam("username") String username,
@QueryParam("age") int age,
@QueryParam("gender") String gender) {
//.....
}
来接收这三个参数。
@PathParam
获取URL路径参数
三、代码 public static final String ImgPath = "D:/Imgaes/";
@Path("/uploadimg")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadImg(@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition dataContentDisposition,
@Context HttpServletRequest request) {
String imgName = Calendar.getInstance().getTimeInMillis() + dataContentDisposition.getFileName();
File file = new File(ImgPath + imgName);
try {
FileUtils.copyInputStreamToFile(fileInputStream, file);
} catch (IOException e) { e.printStackTrace();
}
JsonBuilder resultJson = new JsonBuilder();
resultJson.append("ret", request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + "/app/images/" + imgName);
return resultJson.toString();
} @Path("/images/{name}.{type}")
@GET
public void showImg(@PathParam("name") String imageName,
@PathParam("type") String type,
@Context HttpServletResponse response)
throws IOException {
InputStream inputStream = null;
OutputStream out = null;
try {
File file = new File(ImgPath + imageName + "." + type);
inputStream = new FileInputStream(file);
out = response.getOutputStream();
// pic size = 1M
byte[] bytes = new byte[1024 * 1024];
int len = 0;
while ((len = inputStream.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null)
inputStream.close();
if (out != null)
out.close();
}
}
public static final String ImgPath = "D:/Imgaes/";
@Path("/uploadimg")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadImg(@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition dataContentDisposition,
@Context HttpServletRequest request) {
String imgName = Calendar.getInstance().getTimeInMillis() + dataContentDisposition.getFileName();
File file = new File(ImgPath + imgName);
try {
FileUtils.copyInputStreamToFile(fileInputStream, file);
} catch (IOException e) { e.printStackTrace();
}
JsonBuilder resultJson = new JsonBuilder();
resultJson.append("ret", request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + "/app/images/" + imgName);
return resultJson.toString();
} @Path("/images/{name}.{type}")
@GET
public void showImg(@PathParam("name") String imageName,
@PathParam("type") String type,
@Context HttpServletResponse response)
throws IOException {
InputStream inputStream = null;
OutputStream out = null;
try {
File file = new File(ImgPath + imageName + "." + type);
inputStream = new FileInputStream(file);
out = response.getOutputStream();
// pic size = 1M
byte[] bytes = new byte[1024 * 1024];
int len = 0;
while ((len = inputStream.read(bytes)) > 0) {
out.write(bytes, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null)
inputStream.close();
if (out != null)
out.close();
}
}







