zoukankan      html  css  js  c++  java
  • 【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路径参数

    三、代码

     1 public static final String ImgPath = "D:/Imgaes/";
     2 @Path("/uploadimg")
     3     @POST
     4     @Produces(MediaType.APPLICATION_JSON)
     5     @Consumes(MediaType.MULTIPART_FORM_DATA)
     6     public String uploadImg(@FormDataParam("file") InputStream fileInputStream,
     7                             @FormDataParam("file") FormDataContentDisposition dataContentDisposition,
     8                             @Context HttpServletRequest request) {
     9         String imgName = Calendar.getInstance().getTimeInMillis() + dataContentDisposition.getFileName();
    10         File file = new File(ImgPath + imgName);
    11         try {
    12             FileUtils.copyInputStreamToFile(fileInputStream, file);
    13         } catch (IOException e) {
    14 
    15             e.printStackTrace();
    16         }
    17         JsonBuilder resultJson = new JsonBuilder();
    18         resultJson.append("ret", request.getScheme() + "://" + request.getServerName() + ":"
    19                 + request.getServerPort() + "/app/images/" + imgName);
    20         return resultJson.toString();
    21     }
    22 
    23     @Path("/images/{name}.{type}")
    24     @GET
    25     public void showImg(@PathParam("name") String imageName,
    26                         @PathParam("type") String type,
    27                         @Context HttpServletResponse response)
    28             throws IOException {
    29         InputStream inputStream = null;
    30         OutputStream out = null;
    31         try {
    32             File file = new File(ImgPath + imageName + "." + type);
    33             inputStream = new FileInputStream(file);
    34             out = response.getOutputStream();
    35             // pic size = 1M
    36             byte[] bytes = new byte[1024 * 1024];
    37             int len = 0;
    38             while ((len = inputStream.read(bytes)) > 0) {
    39                 out.write(bytes, 0, len);
    40             }
    41         } catch (Exception e) {
    42             e.printStackTrace();
    43         } finally {
    44             if (inputStream != null)
    45                 inputStream.close();
    46             if (out != null)
    47                 out.close();
    48         }
    49     }
  • 相关阅读:
    知多少进程?
    提高.NET应用性能
    战术设计DDD
    win7下exe文件设置为开机启动
    CQRS项目
    DDD总览
    ML.Net Model Builder
    MySQL主主复制搭建教程收集(待实践)
    MySQL主从复制搭建教程收集(待实践)
    MySQL集群方案收集
  • 原文地址:https://www.cnblogs.com/puyangsky/p/5390263.html
Copyright © 2011-2022 走看看