将本地图片或者网上图片用post方式上传到图片服务器

时间:2021-01-27 12:11:43
一个简单的应用 
Java代码  将本地图片或者网上图片用post方式上传到图片服务器
  1. package com.bj95ol.test;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.File;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.io.InputStreamReader;  
  10. import java.io.OutputStreamWriter;  
  11. import java.io.PrintWriter;  
  12. import java.net.HttpURLConnection;  
  13. import java.net.MalformedURLException;  
  14. import java.net.URL;  
  15.   
  16. import javax.servlet.ServletInputStream;  
  17. import javax.servlet.http.HttpServletRequest;  
  18.   
  19. import sun.misc.BASE64Decoder;  
  20. import sun.misc.BASE64Encoder;  
  21.   
  22. public class SendImgToWeb {  
  23.     /** 
  24.      * 获得网络图片地址。或者图片地址 
  25.      * @param url 
  26.      * @return 
  27.      */  
  28.     public String getContentFromWeb(String url)  
  29.     {  
  30.         String filecontent="";  
  31.         InputStream is=null;  
  32.         BASE64Encoder base=new BASE64Encoder();  
  33.         if(url.startsWith("http"))  
  34.         {  
  35.             try {  
  36.                 HttpURLConnection urlconn=(HttpURLConnection)new URL(url).openConnection();  
  37.                 is=urlconn.getInputStream();  
  38.             } catch (MalformedURLException e) {  
  39.                 // TODO Auto-generated catch block  
  40.                 e.printStackTrace();  
  41.             } catch (IOException e) {  
  42.                 // TODO Auto-generated catch block  
  43.                 e.printStackTrace();  
  44.             }  
  45.         }  
  46.         int n=0;  
  47.         byte[] b=null;  
  48.         try {  
  49.             while((n=is.available())>0)  
  50.             {  
  51.                 n=is.read(b);  
  52.                 if(n==-1)break;  
  53.                 filecontent=filecontent+base.encode(b);  
  54.                   
  55.             }  
  56.             is.close();  
  57.         } catch (IOException e) {  
  58.             // TODO Auto-generated catch block  
  59.             e.printStackTrace();  
  60.         }  
  61.         return filecontent;  
  62.     }  
  63.       
  64.     /** 
  65.      * 将图片内容用post方式发送到url中 
  66.      * @param url 
  67.      * @param postcontent 
  68.      */  
  69.       
  70.     public void sendImgbyPost(String url,String postcontent)  
  71.     {  
  72.         try {  
  73.             HttpURLConnection huc=(HttpURLConnection)new URL(url).openConnection();  
  74.             huc.setDoInput(true);  
  75.             huc.setDoOutput(true);  
  76.             huc.setRequestMethod("POST");  
  77.               
  78.             PrintWriter pw=new PrintWriter(new OutputStreamWriter(huc.getOutputStream()));  
  79.             pw.print(postcontent);  
  80.             pw.close();  
  81.               
  82.             BufferedReader br=new BufferedReader(new InputStreamReader(huc.getInputStream()));  
  83.             String content="";  
  84.             String line=br.readLine();  
  85.             while(line!=null)  
  86.             {  
  87.                 content=content+line;  
  88.                 line=br.readLine();  
  89.                   
  90.             }  
  91.               
  92.         } catch (MalformedURLException e) {  
  93.             // TODO Auto-generated catch block  
  94.             e.printStackTrace();  
  95.         } catch (IOException e) {  
  96.             // TODO Auto-generated catch block  
  97.             e.printStackTrace();  
  98.         }  
  99.           
  100.     }  
  101.     /** 
  102.      * 在服务器端获取发送过来的内容 
  103.      * @param request 
  104.      * @return 
  105.      */  
  106.     public String receiveContent(HttpServletRequest request)  
  107.     {  
  108.         int a = 0;  
  109.          byte[] b = new byte[4096];  
  110.         String result="";  
  111.         try  
  112.         {  
  113.          ServletInputStream sis=request.getInputStream();  
  114.         int line=sis.readLine(b, 0, b.length);  
  115.         while(line!=-1)  
  116.         {  
  117.         result=result+new String(b,0,line);  
  118.         line=sis.readLine(b, 0, b.length);  
  119.         }  
  120.         }  
  121.         catch(Exception e)  
  122.         {  
  123.         e.printStackTrace();  
  124.         }  
  125.         return  result;  
  126.     }  
  127.       
  128.     /** 
  129.      * 将接受过来的信息生成文件 
  130.      * @param request 
  131.      * @param filename 
  132.      */  
  133.     public void createFile(HttpServletRequest request,String filename)  
  134.     {  
  135.         File file=new File(filename);  
  136.         try {  
  137.             FileOutputStream fos=new FileOutputStream(file);  
  138.             String content=receiveContent(request);  
  139.             BASE64Decoder base=new BASE64Decoder();  
  140.             byte[] b=base.decodeBuffer(content);  
  141.             fos.write(b);  
  142.             fos.close();  
  143.               
  144.         } catch (FileNotFoundException e) {  
  145.             // TODO Auto-generated catch block  
  146.             e.printStackTrace();  
  147.         } catch (IOException e) {  
  148.             // TODO Auto-generated catch block  
  149.             e.printStackTrace();  
  150.         }  
  151.           
  152.     }  
  153.   
  154. }