2013-01-03 28 views
0

我試圖存儲一個文件與Apache文件上傳。我的JSP看起來像下面,上傳的文件沒有作爲blob存儲在谷歌應用程序引擎使用java

<form action="/upload" method="post" enctype="multipart/form-data"> 
<input type="file" name="file" /> 
<input type="submit"value="upload" /> 
</form> 

在我的servlet我可以上傳呸如下,

FileService fileService = FileServiceFactory.getFileService(); 
AppEngineFile file = fileService.createNewBlobFile(mime,fileName); 
boolean lock = true; 
byte[] b1 = new byte[BUFFER_SIZE]; 
int readBytes1 = is.read(b1, 0, BUFFER_SIZE); 
while (readBytes1 != -1) { 
writeChannel.write(ByteBuffer.wrap(b1, 0, BUFFER_SIZE));} 
writeChannel.closeFinally(); 

現在嘗試存儲文件使用下面的代碼的BLOB值,

String blobKey = fileService.getBlobKey(file).getKeyString(); 
Entity Input = new Entity("Input"); 
Input.setProperty("Input File", blobKey); 
datastore.put(Input); 

當我嘗試這樣我可以存儲文件名BLOB關鍵,但該文件沒有保存。它在Blob查看器中顯示「0」字節& Google App引擎的Blob列表。

請建議我一個想法來解決這個問題,

您的幫助表示讚賞。

我的servlet

public class UploadServlet extends HttpServlet{ 
    private static final long serialVersionUID = 1L; 
    private static int BUFFER_SIZE =1024 * 1024* 10; 
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
    ServletFileUpload upload = new ServletFileUpload(); 
    FileItemIterator iter; 
try { 
iter = upload.getItemIterator(req); 
while (iter.hasNext()) { 
    FileItemStream item = iter.next(); 
    String fileName = item.getName(); 
    String mime = item.getContentType(); 

    InputStream is = new BufferedInputStream(item.openStream()); 
    try { 
     boolean isMultipart = ServletFileUpload.isMultipartContent(req); 
     if(!isMultipart) { 
      resp.getWriter().println("File cannot be uploaded !");} 
     else { 
      FileService fileService = FileServiceFactory.getFileService(); 
      AppEngineFile file = fileService.createNewBlobFile(mime,fileName); 
      boolean lock = true; 
      FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); 
      byte[] b1 = new byte[BUFFER_SIZE]; 
      int readBytes1; 
      while ((readBytes1 = is.read(b1)) != -1) { 
       writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));} 
       writeChannel.closeFinally(); 
      String blobKey = fileService.getBlobKey(file).getKeyString(); 
      Entity Input = new Entity("Input"); 
      Input.setProperty("Input File", blobKey); 
      datastore.put(Input);}} 
catch (Exception e) { 
     e.printStackTrace(resp.getWriter());} 
    } 
} 

回答

3

您正在閱讀從輸入流中的數據以錯誤的方式。它應該是:

byte[] b1 = new byte[BUFFER_SIZE]; 
int readBytes1; 
while ((readBytes1 = is.read(b1)) != -1) { 
     writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes)); 
} 
writeChannel.closeFinally(); 

更新:你是不是多正確處理 - 它有多個部分,你需要確保你正確讀取部分(名稱爲「文件」部分):

public class UploadServlet extends HttpServlet { 
private static final long serialVersionUID = 1L; 
private static int BUFFER_SIZE = 1024 * 1024 * 10; 

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { 
    ServletFileUpload upload = new ServletFileUpload(); 

    boolean isMultipart = ServletFileUpload.isMultipartContent(req); 
    if (!isMultipart) { 
     resp.getWriter().println("File cannot be uploaded !"); 
     return; 
    } 

    FileItemIterator iter; 
    try { 
     iter = upload.getItemIterator(req); 
     while (iter.hasNext()) { 
      FileItemStream item = iter.next(); 
      String fileName = item.getName(); 
      String fieldName = item.getFieldName(); 
      String mime = item.getContentType(); 

      if (fieldName.equals("file")) { // the name of input field in html 
       InputStream is = item.openStream(); 
       try { 
        FileService fileService = FileServiceFactory.getFileService(); 
        AppEngineFile file = fileService.createNewBlobFile(mime, fileName); 
        boolean lock = true; 
        FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock); 
        byte[] b1 = new byte[BUFFER_SIZE]; 
        int readBytes1; 
        while ((readBytes1 = is.read(b1)) != -1) { 
         writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1)); 
        } 
        writeChannel.closeFinally(); 
        String blobKey = fileService.getBlobKey(file).getKeyString(); 
        Entity input = new Entity("Input"); 
        input.setProperty("Input File", blobKey); 
        datastore.put(input); 
       } catch (Exception e) { 
        e.printStackTrace(resp.getWriter()); 
       } 
      } 
     } 
    } catch (FileUploadException e) { 
     // log error here 
    } 
} 
} 
+0

我試過了,但是它只存儲blob密鑰而不是文件。你能幫我在這 – sathya

+0

你是否使用表格上傳文件?您應該發佈處理POST的servlet的整個代碼。 –

+0

你可以檢查它,我已經發布了我的Servlet文件 – sathya

相關問題