[英]How to upload a File with hibernate-ogm and Mongodb?
I have been able to upload Strings and Dates into a Mongodb, but I cannot figure out how to get a file in there using a webapp. 我已经可以将字符串和日期上传到Mongodb中,但是我无法弄清楚如何使用Webapp在其中获取文件。
I'd rather not use GridFS, but something like a byte[] or a blob (but if I must use GridFS, then, I must). 我宁愿不使用GridFS,而是使用byte []或blob之类的东西(但是,如果必须使用GridFS,则必须使用)。
Here is what I have so far: 这是我到目前为止的内容:
index.jsp: 的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: 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);
}
}
}
How do I get the file in there? 我如何在其中获取文件? I tried
我试过了
request.getParameter("fileContent");
request.getAttribute("fileContent");
but that did not work. 但这没有用。
I was able to upload binary data into my database, but I'm not sure about how to retrieve it. 我能够将二进制数据上传到数据库中,但是我不确定如何检索它。 Here is what I have though:
这是我所拥有的:
Add these lines to my processRequest method: 将这些行添加到我的processRequest方法中:
byte[] b = request.getParameter("fileContent").getBytes();
fileUploadEntity.setFileContent(b);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.