2013-08-23 70 views
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"); 

但是沒有奏效。

+0

爲什麼你會避免使用GridFS的? –

+0

那是當時給我的要求。說實話,你可能不會。 – Jeff

回答

1

我能夠將二進制數據上傳到我的數據庫,但我不確定如何檢索它。這裏是我有什麼,但:

這些行添加到我的processRequest方法:

byte[] b = request.getParameter("fileContent").getBytes(); 


fileUploadEntity.setFileContent(b); 
相關問題