001 | package com.bj95ol.test; |
002 | |
003 | import java.io.BufferedReader; |
004 | import java.io.File; |
005 | import java.io.FileNotFoundException; |
006 | import java.io.FileOutputStream; |
007 | import java.io.IOException; |
008 | import java.io.InputStream; |
009 | import java.io.InputStreamReader; |
010 | import java.io.OutputStreamWriter; |
011 | import java.io.PrintWriter; |
012 | import java.net.HttpURLConnection; |
013 | import java.net.MalformedURLException; |
014 | import java.net.URL; |
015 | |
016 | import javax.servlet.ServletInputStream; |
017 | import javax.servlet.http.HttpServletRequest; |
018 | |
019 | import sun.misc.BASE64Decoder; |
020 | import sun.misc.BASE64Encoder; |
021 | |
022 | public class SendImgToWeb { |
023 | /** |
024 | * 获得网络图片地址。或者图片地址 |
025 | * @param url |
026 | * @return |
027 | */ |
028 | public String getContentFromWeb(String url) |
029 | { |
030 | String filecontent= "" ; |
031 | InputStream is= null ; |
032 | BASE64Encoder base= new BASE64Encoder(); |
033 | if (url.startsWith( "http" )) |
034 | { |
035 | try { |
036 | HttpURLConnection urlconn=(HttpURLConnection) new URL(url).openConnection(); |
037 | is=urlconn.getInputStream(); |
038 | } catch (MalformedURLException e) { |
039 | // TODO Auto-generated catch block |
040 | e.printStackTrace(); |
041 | } catch (IOException e) { |
042 | // TODO Auto-generated catch block |
043 | e.printStackTrace(); |
044 | } |
045 | } |
046 | int n= 0 ; |
047 | byte [] b= null ; |
048 | try { |
049 | while ((n=is.available())> 0 ) |
050 | { |
051 | n=is.read(b); |
052 | if (n==- 1 ) break ; |
053 | filecontent=filecontent+base.encode(b); |
054 | |
055 | } |
056 | is.close(); |
057 | } catch (IOException e) { |
058 | // TODO Auto-generated catch block |
059 | e.printStackTrace(); |
060 | } |
061 | return filecontent; |
062 | } |
063 | |
064 | /** |
065 | * 将图片内容用post方式发送到url中 |
066 | * @param url |
067 | * @param postcontent |
068 | */ |
069 | |
070 | public void sendImgbyPost(String url,String postcontent) |
071 | { |
072 | try { |
073 | HttpURLConnection huc=(HttpURLConnection) new URL(url).openConnection(); |
074 | huc.setDoInput( true ); |
075 | huc.setDoOutput( true ); |
076 | huc.setRequestMethod( "POST" ); |
077 | |
078 | PrintWriter pw= new PrintWriter( new OutputStreamWriter(huc.getOutputStream())); |
079 | pw.print(postcontent); |
080 | pw.close(); |
081 | |
082 | BufferedReader br= new BufferedReader( new InputStreamReader(huc.getInputStream())); |
083 | String content= "" ; |
084 | String line=br.readLine(); |
085 | while (line!= null ) |
086 | { |
087 | content=content+line; |
088 | line=br.readLine(); |
089 | |
090 | } |
091 | |
092 | } catch (MalformedURLException e) { |
093 | // TODO Auto-generated catch block |
094 | e.printStackTrace(); |
095 | } catch (IOException e) { |
096 | // TODO Auto-generated catch block |
097 | e.printStackTrace(); |
098 | } |
099 | |
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 | } |