在struts2中使用MultipartEntityBuilder 在 addPart("file",new FileBody(file))出錯


最近做一個類似於文件中專的功能,前端生成pdf文件,傳到后台,再用httpclient訪問接口發送到別人的服務器,具體代碼如下

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost post = new HttpPost(url); 
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 4000);
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 4000);
//設置不使用長連接

post = new HttpPost(url);
// 構造消息頭
post.addHeader("Content-Type", "multipart/form-data; boundary="+boundary); // 添加請求頭

post.setHeader("Connection", "keep-alive");

//    post.setHeader("Accept-Language","zh-CN,zh;q=0.9"); 
//    post.setHeader("Accept-Encoding","gzip, deflate");
post.setHeader("x-tif-paasid", passid);
post.setHeader("x-tif-uuid",uuid);

String result="";
MultipartEntityBuilder builder = MultipartEntityBuilder.create();

//模擬瀏覽器
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.setBoundary(boundary);
builder.addPart("file",new FileBody(file));
HttpEntity entity = builder.build();
try {
post.setEntity(entity);
HttpResponse response =null;
response = httpClient.execute(post);
// 檢驗返回碼
int statusCode = response.getStatusLine().getStatusCode();
if(statusCode != HttpStatus.SC_OK){
System.out.println("請求出錯:"+statusCode);
}else{
HttpEntity ent = response.getEntity();
result = entityToString(ent);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(post != null){
try {
post.releaseConnection();
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return result;

 


在main方法調用時訪問通暢,但是在ssh的struts2的應用中,運行到 builder.addPart("file",new FileBody(file));這里卡住了,斷點調試的時候進入一個OgnlRuntime.class,不知道是啥意思,不知道有沒有碰到過這個問題的小伙伴


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM