最近做一个类似于文件中专的功能,前端生成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,不知道是啥意思,不知道有没有碰到过这个问题的小伙伴