图片上传

//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 files = new HashMap();  
                  files.put(name, fin);  

                  uploadImg(files,image,imgName); 

}

public void uploadImg(HashMap 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          {  
              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;  
    }  

你可能感兴趣的:(图片上传)