我已经能够将字符串和日期上传到 Mongodb,但我不知道如何使用 webapp 获取文件。
我宁愿不使用 GridFS,而是使用 byte[] 或 blob 之类的东西(但如果我必须使用 GridFS,那么我必须)。
这是我到目前为止所拥有的:
index.jsp:
<form action="./FileUploadServlet" method="POST">
<input type="text" name="subject"/><br>
<input type="date" name="docDate"/>
<%--TODO: accept file into db--%>
<input type="file" name="fileContent"/>
<input type="submit" value="Submit">
</form>
FileUploadServlet.java:
//imports...
@WebServlet(name = "FileUploadServlet", urlPatterns = {"/FileUploadServlet"})
public class FileUploadServlet extends HttpServlet {
throws ServletException, IOException, Exception {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
FileUploadDAO fileUploadDAO = new FileUploadDAO();
FileUploadEntity fileUploadEntity = new FileUploadEntity();
String subject = request.getParameter("subject");
String dateStr = request.getParameter("docDate");
//TODO: GET FILE CONTENTS
Date date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
fileUploadEntity.setSubject(subject);
fileUploadEntity.setDocDate(date);
//TODO: SET FILE CONTENTS
fileUploadDAO.persistAction(fileUploadEntity);
response.sendRedirect("/jdc/index.jsp");
} finally {
out.close();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
processRequest(request, response);
} catch (Exception ex) {
Logger.getLogger(FileUploadServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我怎么把文件放在那里?我试过了
request.getParameter("fileContent");
request.getAttribute("fileContent");
但这没有用。