1
我已經能夠將字符串和日期上載到Mongodb中,但我無法弄清楚如何使用webapp獲取文件。我寧可不使用GridFS,但像一個字節[]或blob(但如果我必須使用GridFS,那麼,我必須)。如何使用hibernate-ogm和Mongodb上傳文件?
這是我到目前爲止有:
的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");
但是沒有奏效。
爲什麼你會避免使用GridFS的? –
那是當時給我的要求。說實話,你可能不會。 – Jeff