0
我有一個包含一個形式被上傳在一個jsp的multipart/form-data的形式發送輸入值到一個servlet
<form action="upload" method="post" enctype="multipart/form-data">
<fieldset>
<input name="nom" class="input-xlarge focused" id="nom" type="text" value="">
<input name="date" class="input-xlarge focused" id="date" type="text" value="">
<input type="file" name="file" />
<button type="submit" class="btn btn-primary">Envoi</button>
</fieldset>
</form>
其中包含2個字段(額定值和日期),也要求輸入文件一個JSP在服務器上。
在servlet的側,我有以下幾點:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String UPLOAD_DIRECTORY = request.getServletContext().getRealPath("/")+"imgs/";
//process only if its multipart content
if(ServletFileUpload.isMultipartContent(request)){
String nom = request.getParameter("nom");
String date = request.getParameter("date");
log.debug("upload parameters: "+nom+" "+date);
try {
List<FileItem> multiparts = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts){
if(!item.isFormField()){
String name = new File(item.getName()).getName();
item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
}
}
//File uploaded successfully
request.setAttribute("message", "File Uploaded Successfully");
log.debug("File updated successfully");
} catch (Exception ex) {
request.setAttribute("message", "File Upload Failed due to " + ex);
log.debug("File upload failed: "+ex);
}
}else{
request.setAttribute("message",
"Sorry this Servlet only handles file upload request");
log.debug("file upload only !");
}
//request.getRequestDispatcher("/result.jsp").forward(request, response);
}
文件上傳工作正常,但我不能使用的request.getParameter取回我的兩個參數(NOM和日期)。
我可以檢索multipart/form-data中的參數嗎?我怎樣才能做到這一點 ?