如何使用Apache FileUppload和HttpClient? [英] How to use Apache FileUppload and HttpClient?
问题描述
如果JAWS应用程序失败,我正在编写一些代码来通过http向服务器发送日志文件。为了测试这个,我设置了一个接收和处理请求的servlet。
I am writing some code to send a log file via http to a server if a JAWS app fails. To test this I have setup a servlet that receives and processes the request.
我的客户端代码来自Apache HttpClient 4.2 examples
My Client code is as from the Apache HttpClient 4.2 examples
{
HttpClient httpclient = new DefaultHttpClient();
try {
HttpPost httppost = new HttpPost("http://localhost:8080/testServer/testserv/a");
FileBody bin = new FileBody(new File("XXXs"));
StringBody comment = new StringBody("A binary file of some kind");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("bin", bin);
reqEntity.addPart("comment", comment);
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println("Response content length: " + resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally {
try { httpclient.getConnectionManager().shutdown(); } catch (Exception ignore) {}
}
}
{
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://localhost:8080/testServer/testserv/a");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("username", "vip"));
nvps.add(new BasicNameValuePair("password", "secret"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
EntityUtils.consume(entity2);
} finally {
httpPost.releaseConnection();
}
}
我的服务器端使用教程 Apache FileUpload
My server side uses the tutorial for Apache FileUpload
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart){
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Parse the request
try {
List<FileItem> items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
File uploadedFile = new File("XXX");
try {
item.write(uploadedFile);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(uploadedFile.getAbsolutePath());
}
} catch (FileUploadException e) {
System.out.println("File Upload Exception");
e.printStackTrace();
}
} else{
System.out.println("Not multi part");
System.out.println(request.getParameterMap());
}
我正在使用JBoss AS7。结果是,如果我不将 FileEntity
添加到 MultiPartEntity
,则处理请求。其他方面抛出了FileUpload异常。
I am using JBoss AS7. The outcome is that if I do not add the FileEntity
to the MultiPartEntity
the request is processed. Other wise a FileUpload exception is thrown.
Processing of multipart/form-data request failed. Stream ended unexpectedly
我在使用JBoss和其他工具方面不是很有经验所以你们需要帮助非常感谢耐心。
I am not very experienced in using JBoss and the other tools here so you're help and patience is greatly appreciated.
推荐答案
从问题我了解到你需要将apache-commons-io作为依赖项。
From this question I learnt that you need to have apache-commons-io as a dependency.
这篇关于如何使用Apache FileUppload和HttpClient?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!