以下 servlet 处理上传照片和该照片的标题。servlet 工作正常(将照片上传到请求的目录),直到添加了处理相同 from 的文本字段的逻辑。现在它既不上传照片也不向数据库提交标题。该表单的编码类型为multipart/form-data
.
HTML
<form method="post" action="UploadHandler" enctype="multipart/form-data">
<table>
<tr>
<td> <strong> Browse photo to submit </strong> </td>
<td> <input type="file" name="ImageToUpload" value="Upload Photo"/> </td>
</tr>
<tr>
<td> <strong> Give a Caption to this photo </strong> </td>
<td> <input type="text" name="CaptionBox" size="40" /></td>
</tr>
<tr>
<td> <strong> Tags <a href="#"> <i> Browse tags </i> </a> </strong> </td>
<td> <input type="text" size="20" value="Tags" id="tagfield" style="color: #A0A0A4; font-size:16px;" onfocus="emptyTheDefaultValue()"> </td>
</tr>
<tr colspan="2">
<td> <input type="submit" value="submit photo"/> </td>
</tr>
</table>
</form>
处理上传的 Servlet
package projectcodes;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.File;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import NonServletFiles.HandleCaption;
import NonServletFiles.ChangePartToString;
public class UploadHandler extends HttpServlet {
@Override
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
response.setContentType("text/plain");
String path = request.getParameter("ImageToUpload");
// Now handle the caption
// Note : They have a multipart/form-data request
// Using the servlet API 3.0
ChangePartToString cpts = new ChangePartToString(request.getPart("CaptionBox")); // get the CaptionBox part
String caption = null;
try {
caption = cpts.getValue();
System.out.println(caption); // <<-------- I get this value fine
}catch(Exception exc) {
exc.printStackTrace();
System.out.println(exc);
}
PrintWriter writer = response.getWriter();
try {
Boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart) {
Boolean AttemptToUploadFile = true;
RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
request.setAttribute("UploadAttempt", AttemptToUploadFile);
rd.forward(request, response);
} else {
DiskFileItemFactory diskFileItem = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(diskFileItem);
List list = null;
try {
list = fileUpload.parseRequest(request);
}catch(Exception exc) {
// Boolean AttemptToUploadFile = true;
// RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
// request.setAttribute("UploadAttempt", AttemptToUploadFile);
// rd.forward(request, response);
exc.printStackTrace();
System.out.println(exc);
}
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
String emailOfTheUser = null;
FileItem fileItem = (FileItem)iterator.next();
if(!fileItem.isFormField()) {
String fieldName = fileItem.getFieldName();
String fileName = FilenameUtils.getName(fileItem.getName());
HttpSession session = request.getSession();
if(!session.isNew()) {
emailOfTheUser = (String)session.getAttribute("Email");
}
File file = new File("/home/non-admin/project uploads/project users/" + emailOfTheUser ,fileName);
fileItem.write(file);
// now since the file has been uploaded , upload the caption to the photo
System.out.println("THE CAPTION :" + caption);
HandleCaption hc = new HandleCaption(caption,emailOfTheUser,fileName);
hc.SubmitCaptionToTheDatabase(); // This calls invokes a method that submits a caption to the database
RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
String message = "File Uploaded successfully !";
request.setAttribute("SuccessMessage", message);
rd.forward(request, response);
}
}
}
}catch(Exception exc) {
exc.printStackTrace();
System.out.println(exc);
}
}
}
字幕字符串返回正常。文件未上传的原因可能是什么。我没有在服务器日志中看到任何异常。