HTTP上传工具
package com.dayo.tool; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods.InputStreamRequestEntity; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.RequestEntity; public class UploadRnd_office{ private final static Integer THREAD_SLEEP_TIME = 1000; //等待rnd_office回复信息的时间 public static void uploadDir(String url,String path,String savePathMark){ File sourceFile = new File(path); File[] fileList = sourceFile.listFiles(); String filename = ""; try{ if(null==fileList || fileList.length==0){ System.out.println("没有文件..."); return; } for(File ff : fileList){ if(ff.isFile()){ filename = getSvnDirByPath(ff.getPath(),savePathMark); uploadFile(url,ff.getPath(),filename); }else if(ff.isDirectory()){ path = ff.getPath(); uploadDir(url,path,savePathMark); } } }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } } public static String getSvnDirByPath(String filePath,String savePathMark){ String saveDir = ""; try{ System.out.println("============================="); filePath = filePath.replace("\\", "#"); savePathMark = savePathMark.replace("\\", "#"); String[] dirs = filePath.split(savePathMark); saveDir = savePathMark+dirs[1]; saveDir = saveDir.replace("#", "/"); System.out.println(saveDir); }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } return saveDir; } public static void uploadFile(String url,String path,String filename){ try{ StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); FileInputStream fi = new FileInputStream(new File(path)); InputStreamRequestEntity fr = new InputStreamRequestEntity(fi); method.setRequestEntity((RequestEntity)fr); method.setRequestHeader("filename", filename); try{ client.executeMethod(method);//执行上传操作 if(method.getStatusCode() == HttpStatus.SC_OK){ //读取为 InputStream,在网页内容数据量大时候推荐使用 BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), "GBK")); String line; while ((line = reader.readLine())!= null) { response.append(line); } reader.close(); } }catch(Exception e){ System.out.println("执行HTTP Post请求" + url + "时,发生异常!"); e.printStackTrace(); }finally{ method.releaseConnection(); } System.out.println("--------------------"+response.toString()); }catch(Exception e){ throw new RuntimeException("文件上传失败!!"); } } public static String uploadMessage(String urlString,String message){ OutputStream os = null; PrintWriter out = null; try{ URL url = new URL(urlString); URLConnection ucon = url.openConnection(); HttpURLConnection httpurl = (HttpURLConnection)ucon; httpurl.setRequestMethod("GET"); httpurl.setDoInput(true); httpurl.setDoOutput(true); httpurl.setRequestProperty("Content-Encoding", "gbk"); httpurl.setRequestProperty("name", "mozishun"); httpurl.connect(); StringBuffer sb = new StringBuffer(); String lengthStr = new Integer(message.length()).toString(); sb.append("sgpcba").append("\n"); sb.append(lengthStr).append("\n"); sb.append(message); message = sb.toString(); os = httpurl.getOutputStream(); out = new PrintWriter(os); out.print(message); out.flush(); System.out.println("连接结果:"+httpurl.getResponseMessage()); if(httpurl.getResponseMessage().equals("OK")){ Thread.sleep(UploadRnd_office.THREAD_SLEEP_TIME); return getReply(httpurl); }else{ return "连接"+urlString+"失败!!"; } }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); }finally{ try{ if(os!=null) os.close(); if(out!=null) out.close(); }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); } } } public static String uploadObject(String urlString,Object obj){ OutputStream os = null; ObjectOutputStream out = null; try{ URL url = new URL(urlString); URLConnection ucon = url.openConnection(); HttpURLConnection httpurl = (HttpURLConnection)ucon; httpurl.setRequestMethod("GET"); httpurl.setDoInput(true); httpurl.setDoOutput(true); httpurl.setRequestProperty("Content-Encoding", "gbk"); httpurl.setRequestProperty("name", "mozishun"); httpurl.connect(); os = httpurl.getOutputStream(); out = new ObjectOutputStream(os); out.writeObject(obj); out.flush(); System.out.println("连接结果:"+httpurl.getResponseMessage()); if(httpurl.getResponseMessage().equalsIgnoreCase("OK")){ Thread.sleep(UploadRnd_office.THREAD_SLEEP_TIME); return getReply(httpurl); }else{ return "连接"+urlString+"失败!!"; } }catch(Exception e){ e.printStackTrace(); throw new RuntimeException(e); }finally{ try{ if(os!=null)os.close(); if(out!=null)out.close(); }catch(Exception e){ e.printStackTrace(); } } } public static String getReply(HttpURLConnection httpurl)throws Exception{ InputStream is = httpurl.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String request = reader.readLine(); if(request.equalsIgnoreCase("ok")){ System.out.println("上传成功!!"); }else{ System.out.println("上传失败,失败原因:"+request); } return request; } public static void main(String[] args)throws Exception{ //UploadRnd_office.uploadFile("http://172.16.0.11:8002/receive","D:\\Temp\\mozishun\\test.txt","mo.txt"); //UploadRnd_office.uploadDir("http://172.16.0.11:8002/receive","D:\\Temp\\mozishun\\Dayo_Factory\\S300\\SW\\S300_H08_S01\\S300_H08_S01_R2041","\\S300\\"); UploadRnd_office.uploadDir("http://172.16.0.11:8002/receive","D:\\Temp\\mozishun\\Dayo_Factory\\S300\\HW\\S300_H01_P5","\\S300\\"); //UploadRnd_office.uploadMessage("http://172.16.0.11:8222/rnd_office/sgservlet?test=test", "I come from sg server"); /*UserInfo userInfo = new UserInfo(); userInfo.setUserName("liufei"); userInfo.setPassword("123456"); UploadRnd_office.uploadObject("http://172.16.0.11:8222/rnd_office/sgservlet?test=test", userInfo);*/ } }
相关推荐
Kafka 2020-09-18
Wepe0 2020-10-30
杜倩 2020-10-29
windle 2020-10-29
minerd 2020-10-28
mengzuchao 2020-10-22
Junzizhiai 2020-10-10
bxqybxqy 2020-09-30
风之沙城 2020-09-24
kingszelda 2020-09-22
大唐帝国前营 2020-08-18
yixu0 2020-08-17
TangCuYu 2020-08-15
xiaoboliu00 2020-08-15
songshijiazuaa 2020-08-15
xclxcl 2020-08-03
zmzmmf 2020-08-03
newfarhui 2020-08-03
likesyour 2020-08-01