图片上传

//App上传图片到App后端 App后端将图片传给PC项目的文件夹中

App将图片转为base64 以字符串形式传给App后端

//App后端接受

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// 定义变量存储图片地址
        String imagePath = "";
        // 接收图片数据   (base64)

        String image = request.getParameter("img");

         //得到存储的地址

      Properties pps = new Properties();
      pps.load(this.getClass().getClassLoader().getResourceAsStream("/img.properties"));
      String url=pps.getProperty("url");
      // 图片输出路径
      String imgName=UUID.randomUUID().toString();
      String name="";
       if("1".equals(state)){
              name=imgName+".png";
              imgName="/fileDisk/wa/ppsend/"+imgName+ ".png";
              }else{
              name=imgName+".png";
              imgName="/fileDisk/wa/pprec/"+imgName+ ".png";
              }
              imagePath=url+imgName;
      // 将base64 转 字节数组
              byte[] decode = Base64.getDecoder().decode(image);
              // 定义图片输入流1
               InputStream fin = new ByteArrayInputStream(decode);
                  //上传到远程服务器  
                 HashMap<String, InputStream> files = new HashMap<String, InputStream>();  
                  files.put(name, fin);  

                  uploadImg(files,image,imgName); 

}

public void uploadImg(HashMap<String, InputStream> files,String image,String adress) throws Exception {  
        //地址
        Properties pps = new Properties();
pps.load(this.getClass().getClassLoader().getResourceAsStream("/img.properties"));
String s=pps.getProperty("url");
        URL url = new URL(s+请求地址); 
    String param="image="+URLEncoder.encode(image,"UTF-8");
     param+="&adress="+URLEncoder.encode(adress,"UTF-8");
    HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
    //设置参数
    httpConn.setDoOutput(true);   //需要输出
    httpConn.setDoInput(true);   //需要输入
    httpConn.setUseCaches(false);  //不允许缓存
    httpConn.setRequestMethod("POST");   //设置POST方式连接
    //设置请求属性

    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            // 维持长连接

             httpConn.setRequestProperty("Connection", "Keep-Alive");

            httpConn.setRequestProperty("Charset", "UTF-8");

    httpConn.connect();

            //输出流

    DataOutputStream dos=new DataOutputStream(httpConn.getOutputStream());
    dos.writeBytes(param);
    dos.flush();
    dos.close();
    //获得响应状态
    int resultCode=httpConn.getResponseCode();
    if(HttpURLConnection.HTTP_OK==resultCode){
StringBuffer sb=new StringBuffer();
 String readLine=new String();
BufferedReader responseReader=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),"UTF-8"));
      while((readLine=responseReader.readLine())!=null){
        sb.append(readLine).append("\n");
      }
      responseReader.close();
    }
    }  

//PC接收

  @RequestMapping("/receiveImg")  
    public String receive(HttpServletRequest request,HttpServletResponse response) throws Exception {  
// 判断enctype属性是否为multipart/form-data  
String image=request.getParameter("image");
String adress=request.getParameter("adress");
byte[] decode = Base64.getDecoder().decode(image);;
     // 定义图片输入流
     InputStream fin = new ByteArrayInputStream(decode);
     String ppSendphoto=request.getSession().getServletContext().getRealPath(adress);
          for(int i=0;i<decode.length;++i)  
          {  
              if(decode[i]<0)  
              {//调整异常数据  
              decode[i]+=256;  
              }  
          } 
        OutputStream ou = new FileOutputStream(ppSendphoto);
        ou.write(decode);
        ou.flush();
        fin.close();
        ou.close();
        response.setCharacterEncoding("UTF-8");    
        response.getWriter().println("上传成功");  
        return null;  
    }  

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值