【发布时间】:2012-01-06 01:50:05
【问题描述】:
我编写了一个使用 Apache 公用文件上传库处理文件上传的 Servlet。以下是部分代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
try {
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
// Set size threshold for storing upload
fileItemFactory.setSizeThreshold(1 * 1024 * 50); // 50 KB
// Set temporary directory to store uploaded files above threshold size
fileItemFactory.setRepository(new File(TEMP_DIRECTORY));
ServletFileUpload upload = new ServletFileUpload(fileItemFactory);
//HashMap<String, String> params = new HashMap<String, String>();
FileItemIterator iterator = upload.getItemIterator(request);
upload.setSizeMax(REQUEST_MAX_SIZE);
List items = upload.parseRequest(request);
Iterator it = items.iterator();
while (it.hasNext()) {
FileItem item = (FileItem) it.next();
if(item.isFormField()) {
} else {
String contentType = item.getContentType();
String fileName = item.getName();
String fieldName = item.getFieldName();
boolean isInMemory = item.isInMemory();
long sizeInBytes = item.getSize();
File uploadedFile = new File(PATH + "new_audio1.amr");
item.write(uploadedFile);
System.out.println("Field: " + fieldName);
System.out.println("File name: " + fileName);
System.out.println("Size: " + sizeInBytes);
System.out.println("Is in memory:" + isInMemory);
}
}
} catch (Exception ex) {
throw new ServletException(ex);
}
} else {
throw new ServletException();
}
由于某些让我无法理解的原因,列表“项目”是空的,所以我无法获取上传的文件。
对于上传本身,我写了一些java代码:
File audioFile = new File("C:\\Users\\Soto\\Desktop\\test recording.amr");
String url = "http://localhost:8080/AudioFileUpload/UploadServlet";
String charset = "UTF-8";
// random values
String latitude = "145";
String longitude = "132";
String speed = "0";
String query;
try {
query = String.format("latitude=%s&longitude=%s&speed=%s", URLEncoder.encode(latitude, charset), URLEncoder.encode(longitude, charset), URLEncoder.encode(speed, charset));
} catch (UnsupportedEncodingException e) {
query = String.format("latitude=%s&longitude=%s&speed=%s", latitude, longitude, speed);
}
HttpClient httpClient = new DefaultHttpClient();
httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httpPost = new HttpPost(url + "?" + query);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(audioFile, "audio/AMR");
mpEntity.addPart("audioFile", cbFile);
httpPost.setEntity(mpEntity);
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
System.out.println(response.getStatusLine());
if(responseEntity != null)
System.out.println(EntityUtils.toString(responseEntity));
if(responseEntity != null) {
EntityUtils.consume(responseEntity);
}
httpClient.getConnectionManager().shutdown();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在我看来,文件已正确附加和上传。 我也尝试通过 HTML 使用 multipart/form-data 发布请求,但仍然找不到该文件。
我做错了什么?
编辑: 我删除了 doPost() 开头的行以及 if 语句:
ServletFileUpload.isMultipartContent(request);
然后上传工作正常。此方法是否有可能消耗请求的“输入/输出/无论它是什么”流?
谢谢
【问题讨论】:
-
您尝试从浏览器上传吗?
标签: java servlets file-upload httpclient