resteasy 上传图片

用作备忘,
代码如下:
  @POST
  @Path("upload")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  @Produces(MediaType.APPLICATION_JSON)
  public JSONObject upload1(MultipartFormDataInput formDataInput) {
    JSONObject json = new JSONObject();
    Integer isSuccess = Constants.INTEGER_ZERO;
    FileOutputStream fop = null;
    InputStream inputStream = null;
    try {
      //提交的form表单
      Map<String, List<InputPart>> uploadForm = formDataInput.getFormDataMap();
      //      //如果有中文参数的话,用下面方法防止乱码
      //      //中文表单数据
      //      InputPart zhongweninputParts = uploadForm.get("title").get(0);
      //      //解决中文乱码
      //      zhongweninputParts.setMediaType(MediaType.TEXT_PLAIN_TYPE);
      //图片
      InputPart imgfileInputParts = uploadForm.get("imgfile").get(0);
      inputStream = imgfileInputParts.getBody(InputStream.class, null);
      String img = "D:\\" + System.currentTimeMillis() + ".png";
      byte[] bytes = IOUtils.toByteArray(inputStream);
      fop = new FileOutputStream(img);
      fop.write(bytes);
      json.put("img", img);
    } catch (Exception e) {
      json.put("isSuccess", Constants.INTEGER_ZERO.toString());
      logger.info("上传图片出错");
      logger.info(e);
    } finally {
      try {
        fop.flush();
        fop.close();
        inputStream.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      json.put("isSuccess", isSuccess.toString());
    }
    return json;
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值