以下 HTML 片段将文件提交给作为 servlet 的UploadHandler。然后还有一个标题框也需要处理。我可以在UploadHandler中处理标题框,然后打开与数据库的连接并在那里提交。但我不想这样做。让上传处理程序处理文件的上传。那么有什么选择呢?我如何将标题提交到表格中?我想在处理这两个工作时创造一种并行感。
<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="caption box" size="40" /></td>
</tr>
<tr colspan="2">
<td> <input type="submit" value="submit photo"/> </td>
</tr>
</table>
</form>
当我点击提交 2 个不同的作业时,有什么方法可以让它们由 2 个不同的 servlet 处理?从UploadaHandler创建一个新线程似乎不是一个好主意。
在@Luiggi Mendoza 发表评论后:
处理文件上传的 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;
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");
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);
}
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);
RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
String message = "File Uploaded successfully !";
request.setAttribute("SuccessMessage", message);
rd.forward(request, response);
}
}
}
}catch(Exception exc) {
Boolean AttemptToUploadFile = true;
RequestDispatcher rd = request.getRequestDispatcher("portfolio_one.jsp");
request.setAttribute("UploadAttempt", AttemptToUploadFile);
rd.forward(request, response);
}
}
}