转自:http://blog.csdn.net/lianghongge/article/details/42120751
http://www.oschina.net/code/snippet_1864608_37835
通过httpclient的上传文件,可以处理跨域上传的问题,比如先上传到A服务器的某个临时文件目录下(临时目录下的文件,可以定时清理,通过编写shell(参考此篇),或者使用spring-quartz(参考此篇)等),然后再将文件传给其他域下的B服务,B服务处理成功后,将相对路径等信息传给A服务,A服务再保存相对路径,也可以处理预览图片等。
代码1:
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.mime.HttpMultipartMode; import org.apache.http.entity.mime.MultipartEntityBuilder; import org.apache.http.entity.mime.content.FileBody; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.CharsetUtils; import org.apache.http.util.EntityUtils; /** * */ /** * @author zhzh 2015-3-31 */ public class Main { /** * @param args * @throws ClassNotFoundException */ public static void main(String[] args) throws ClassNotFoundException { try { postFile(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 上传文件 * * @throws ParseException * @throws IOException */ public static void postFile() throws ParseException, IOException { CloseableHttpClient httpClient = HttpClients.createDefault(); try { // 要上传的文件的路径 String filePath = new String("C:\\Users\\zz\\Desktop\\diqu.png"); // 把一个普通参数和文件上传给下面这个地址 是一个servlet HttpPost httpPost = new HttpPost( "http://11.2.2.223:8081/auth_center/upload"); // 把文件转换成流对象FileBody File file = new File(filePath); FileBody bin = new FileBody(file); /*StringBody uploadFileName = new StringBody("my.png", ContentType.create("text/plain", Consts.UTF_8));*/ // 以浏览器兼容模式运行,防止文件名乱码。 HttpEntity reqEntity = MultipartEntityBuilder.create() .setMode(HttpMultipartMode.BROWSER_COMPATIBLE) .addPart("uploadFile", bin) // uploadFile对应服务端类的同名属性<File类型> //.addPart("uploadFileName", uploadFileName)// uploadFileName对应服务端类的同名属性<String类型> .setCharset(CharsetUtils.get("UTF-8")).build(); httpPost.setEntity(reqEntity); System.out.println("发起请求的页面地址 " + httpPost.getRequestLine()); // 发起请求 并返回请求的响应 CloseableHttpResponse response = httpClient.execute(httpPost); try { // 打印响应状态 System.out.println(response.getStatusLine()); // 获取响应对象 HttpEntity resEntity = response.getEntity(); if (resEntity != null) { // 打印响应长度 System.out.println("Response content length: " + resEntity.getContentLength()); // 打印响应内容 System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8"))); } // 销毁 EntityUtils.consume(resEntity); } finally { response.close(); } } finally { httpClient.close(); } } /** * 下载文件 * * @param url * http://www.xxx.com/img/333.jpg * @param destFileName * xxx.jpg/xxx.png/xxx.txt * @throws ClientProtocolException * @throws IOException */ public static void getFile(String url, String destFileName) throws ClientProtocolException, IOException { // 生成一个httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); File file = new File(destFileName); try { FileOutputStream fout = new FileOutputStream(file); int l = -1; byte[] tmp = new byte[1024]; while ((l = in.read(tmp)) != -1) { fout.write(tmp, 0, l); // 注意这里如果用OutputStream.write(buff)的话,图片会失真,大家可以试试 } fout.flush(); fout.close(); } finally { // 关闭低层流。 in.close(); } httpclient.close(); } }
代码2:
/*** * 上传图片操作 * @param request * @param response * @return * @throws IllegalStateException * @throws IOException */ @SuppressWarnings({ "rawtypes", "null" }) @RequestMapping(value = "upload", produces = "text/plain;charset=UTF-8") @ResponseBody public String ajaxUpload(HttpServletRequest request, HttpServletResponse response) throws IllegalStateException, IOException { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; String rootPath = request.getSession().getServletContext().getRealPath("/"); String fileName = ""; String path = rootPath+"/resource/template/"; for (Iterator it = multipartRequest.getFileNames(); it.hasNext();) { String key = (String) it.next(); MultipartFile mulfile = multipartRequest.getFile(key); fileName = mulfile.getOriginalFilename(); File file = new File(path + fileName); mulfile.transferTo(file); } JSONObject o = new JSONObject(); o.put("fileName", "/resource/template/"+fileName); return o.toString(); }
结果:文件上传到服务器后,返回信息打印如下
发起请求的页面地址 POST http://11.2.2.223:8081/auth_center/upload HTTP/1.1
HTTP/1.1 200 OK
Response content length: 42
{"fileName":"/resource/template/diqu.png"}