Apache commons 파일 업로드 라이브러리를 사용하여 파일 업로드를 처리하는 서블릿을 작성했습니다.서블릿이 다중 요청에서 FileItem을 찾지 못하는 이유는 무엇입니까?
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();
}
나에게 목록 '항목을'탈출 그래서 업로드 된 파일을 잡을 수없는 비어있는 몇 가지 이유를 들어 다음 코드의 일부이다. 업로드 자체를 들어
, 나는 몇 가지 자바 코드를 작성했습니다 : 파일이 첨부 제대로 업로드되는 것처럼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();
}
그것은 나에게 느낀다. 또한 multipart/form-data 게시 요청으로 HTML을 통해 작업을 시도했지만 여전히 파일을 찾을 수 없습니다.
내가 뭘 잘못하고 있니?
편집
: 가 나는 경우 성명과 함께의 doPost()의 시작 부분에 라인을 제거 :ServletFileUpload.isMultipartContent(request);
그리고 업로드가 제대로 작동했다. 이 메소드가 요청의 '입력/출력/무엇이든지간에'스트림을 소비 할 수 있습니까?
감사합니다.
브라우저에서 업로드를 시도 했습니까? –