httpclient 從服務器A 上傳文件到服務器B


//服務器A:

public String HttpUploadFile() throws HttpException, IOException{

//服務器B的地址如:http://192.168.5.12:8080/exeProject

String upCascadeWebUrl = getHttpSession().getAttribute("upCascadeWebUrl").toString();

//獲取form表單或地址欄的請求參數

Map<String,String[]> mparams = getHttpRequest().getParameterMap();
String actionUrl = upCascadeWebUrl+"/CascadeSuc.do";

PostMethod postMethod = null;

//提交表單數據不含文件

if(upfile==null){
//級聯地址
postMethod = new PostMethod(actionUrl);
for (String key : mparams.keySet()) {
postMethod.setParameter(key, mparams.get(key)[0]);
}
postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

}else{
//文件上傳本地服務器
FileInputStream fins = new FileInputStream(upfile);
String filePath = getRealPath("/upload")+"\\"+upfileFileName;
FileOutputStream fout = new FileOutputStream(filePath);
byte[]bts = new byte[1024];
int count = 0;
while((count=fins.read(bts))!=-1){
fout.write(bts,0,count);

}
fins.close();
fout.close();
//文件跨域上傳
postMethod = new PostMethod(actionUrl);
upfile = new File(filePath);
//文件參數,參數"UTF-8"(和頁面提交字符集編碼對應)解決文件名中文亂碼問題
FilePart fp = new FilePart("upfile",upfile,null,"UTF-8");
//普通參數上傳,參數"UTF-8"解決中文亂碼問題
StringPart flowType = new StringPart("flowType", mparams.get("flowType")[0], "UTF-8");
StringPart globalDomainId = new StringPart("globalDomainId", mparams.get("globalDomainId")[0], "UTF-8");
StringPart sendUser = new StringPart("sendUser", mparams.get("sendUser")[0], "UTF-8");
StringPart processPlatName = new StringPart("processPlatName", mparams.get("processPlatName")[0], "UTF-8");
StringPart dealDomain = new StringPart("dstPlateform", mparams.get("dstPlateform")[0], "UTF-8");
StringPart isSendMsg = new StringPart("isSendMsg", mparams.get("isSendMsg")[0], "UTF-8");
StringPart ip = new StringPart("ip", mparams.get("ip")[0], "UTF-8");
StringPart mac = new StringPart("mac", mparams.get("mac")[0], "UTF-8");
StringPart register = new StringPart("register", mparams.get("register")[0], "UTF-8");
StringPart occuredTime = new StringPart("occuredTime", mparams.get("occuredTime")[0], "UTF-8");
StringPart eventName = new StringPart("eventName", mparams.get("eventName")[0], "UTF-8");
StringPart content = new StringPart("content", mparams.get("content")[0],"UTF-8");
StringPart fileName = new StringPart("fileName", upfileFileName,"UTF-8");
StringPart eventSource = new StringPart("eventSource", mparams.get("eventSource")[0],"UTF-8");
Part[] parts = {flowType,globalDomainId,sendUser,processPlatName,dealDomain,isSendMsg,ip,mac,register,occuredTime,eventName,content,fileName,eventSource,fp};
MultipartRequestEntity mre = new MultipartRequestEntity(parts,postMethod.getParams());
postMethod.setRequestEntity(mre);
}
postMethod.setRequestHeader("cascade", "1");//設置消息頭
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);
int status = client.executeMethod(postMethod);
if(status == HttpStatus.SC_OK){
String responseText = postMethod.getResponseBodyAsString();
getResponse().setContentType("text/html;charset=UTF-8");
getResponse().getWriter().write(responseText);
postMethod.releaseConnection();
}else{
String responseText = "失敗!";
getResponse().setContentType("text/html;charset=UTF-8");
getResponse().getWriter().write(responseText);
}
return NONE;

}




//服務器B:

public String CascadeSuc() throws IOException{
String msg = "";
String attachment = "";
int flag = 0;

Map<String,String[]> params = getHttpRequest().getParameterMap();
Map<String,Object> mparams = new HashMap<String, Object>();
for(String key:params.keySet()){
mparams.put(key,params.get(key)[0]);
}
if(upfile!=null){
Date date = new Date();
upfileFileName = mparams.get("fileName").toString();
upfileFileName = upfileFileName.substring(0, upfileFileName.lastIndexOf("."))+date.getTime()+upfileFileName.substring(upfileFileName.lastIndexOf("."));
// i:上傳成功與否;-1代表失敗,1代表成功
// 打包的zip文件名為"上傳文件名_日期.zip",防止上傳時文件名重復后發生覆蓋
String saveFileName = upfileFileName.substring(0, upfileFileName.lastIndexOf("."))+".zip";
flag = uploadProcessFile(upfile,saveFileName,upfileFileName);
attachment = "upload/"+ saveFileName;
if(flag == 2){
msg = "上傳文件超過30M限制!";
msg = "<div id='msgInfo'>"+msg+"</div>";
getResponse().setContentType("text/html");
getResponse().getWriter().write(msg);
return NONE;

}if(flag == -1){
msg = "文件上傳失敗!";
msg = "<div id='msgInfo'>"+msg+"</div>";
getResponse().setContentType("text/html");
getResponse().getWriter().write(msg);
return NONE;
}

}
//根據需求參數處理



DBRecordSet dbrs = new DBRecordSet();
executeXmlSQL(dbrs, "creatPreAlarm@process", mparams);

if(dbrs.getAffectRowCount()>0){
msg = "成功!";
}else{
msg = "失敗!";
}
msg = "<div id='msgInfo'>"+msg+"</div>";
getResponse().setContentType("text/html");
getResponse().getWriter().write(msg);
return NONE;
}

//文件上傳方法

public int uploadProcessFile(upfile,saveFileName,upfileFileName){

return flag;

//文件上傳

}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com