上传的文件不使用java在谷歌应用引擎中存储为blob

时间:2013-01-03 06:50:15

标签: java google-app-engine

尝试使用Apache文件上传来存储文件。我的JSP如下所示,

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

在我的Servlet中,我可以获得上传的fie,

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键,但文件没有存储。它告诉我&#34; 0&#34; Blob查看器中的字节数和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());}
  }
}

1 个答案:

答案 0 :(得分:3)

您正在以错误的方式从输入流中读取数据。它应该是:

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

更新:您没有正确处理多部分 - 它有多个部分,您需要确保阅读正确的部分(部分名称为“file”):

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
    }
}
}