BASE64发送图片和保存图片

服务器端 方法内代码

Java代码   收藏代码
  1. String fileName = request.getParameter("fileName");  
  2. String str = request.getParameter("file");  
  3. log.info(fileName+"------------");  
  4. log.info(str+"-==================");               
  5. BASE64Decoder decoder = new BASE64Decoder();  
  6.             try {  
  7.                 // Base64解码  
  8.                 byte[] bytes = decoder.decodeBuffer(str);  
  9.                 for (int i = 0; i < bytes.length; ++i) {  
  10.                     if (bytes[i] < 0) {// 调整异常数据  
  11.                         bytes[i] += 256;  
  12.                     }  
  13.                 }  
  14.                 // 生成图片  
  15.                 OutputStream outs = new FileOutputStream(filepath+fileName);  
  16.                 outs.write(bytes);  
  17.                 outs.flush();  
  18.                 outs.close();  
  19.                 return null;  
  20.             } catch (Exception e) {  
  21.                      logger.error("", e);  
  22.             }  

 

客户调用端

Java代码   收藏代码
  1. public static String getPicBASE64() {  
  2.         String picPath = "/var/xxxx.png";  
  3.         String content = "";  
  4.         try {  
  5.             FileInputStream fileForInput = new FileInputStream(picPath);  
  6.             byte[] bytes = new byte[fileForInput.available()];  
  7.             if(bytes.length<102400){  
  8.                 System.out.print(bytes.length);  
  9.             }  
  10.             fileForInput.read(bytes);  
  11.         content = new sun.misc.BASE64Encoder().encode(bytes); // 具体的编码方法  
  12.             fileForInput.close();  
  13.         } catch (Exception e) {  
  14.             e.printStackTrace();  
  15.         }  
  16.         return content;  
  17.     }  
  18.   
  19.     public static void main(String[] args) throws Exception {  
  20.   
  21.   
  22.         URL url = new URL("http://**********:80/newdemo/app/imageUpload.do");  
  23.         HttpURLConnection connection = (HttpURLConnection) url.openConnection();  
  24.         connection.setDoOutput(true);  
  25.         connection.setDoInput(true);  
  26.         // Post 请求不能使用缓存  
  27.         connection.setUseCaches(false);  
  28. // URLConnection.setFollowRedirects是static函数,作用于所有的URLConnection对象。  
  29.         // connection.setFollowRedirects(true);  
  30.         // URLConnection.setInstanceFollowRedirects是成员函数,仅作用于当前函数  
  31.         connection.setInstanceFollowRedirects(false);  
  32.         // 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的  
  33. // 正文是urlencoded编码过的form参数,下面我们可以看到我们对正文内容使用URLEncoder.encode  
  34.         // 进行编码  
  35.         connection.setRequestProperty("Content-Type",  
  36.                 "application/x-www-form-urlencoded");  
  37.         // 连接,从postUrl.openConnection()至此的配置必须要在connect之前完成,  
  38.         // 要注意的是connection.getOutputStream会隐含的进行connect。  
  39.         connection.connect();  
  40.         Long sendTime = System.currentTimeMillis();  
  41.         DataOutputStream out = new DataOutputStream(  
  42.                 connection.getOutputStream());  
  43.         // 要传的参数  
  44.         String content ="fileName=ssh2.jpg";  
  45.         // 得到图片的base64编码  
  46.         content = content + "&" + URLEncoder.encode("file""UTF-8") + "="+ URLEncoder.encode(getPicBASE64(), "UTF-8");  
  47.         out.writeBytes(content);  
  48.         out.flush();  
  49.         out.close();   
  50.             }  
  51.   
  52.          asdfasdfasdf 
  53.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值