Google App Engine / Java中的多文件上载在开发机器中工作,但在部署时不工作

时间:2012-05-23 01:14:42

标签: google-app-engine blobstore

这是我的servlet的一部分:

public void doPost(HttpServletRequest req, HttpServletResponse res)
    throws ServletException, IOException {

    @SuppressWarnings("deprecation")
    Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(req);

    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();

    ServletFileUpload upload = new ServletFileUpload();
    FileItemIterator iterator;

    try {
        iterator = upload.getItemIterator(req);
        Picture pic = null;
        PictureAccess access = null;
        while(iterator.hasNext()){
            FileItemStream item = iterator.next(); 

            pic = new Picture( blobs.get(item.getFieldName()).getKeyString() );

            access = new PictureAccess();
            access.addPictures(pic, user.getEmail() );
        }

    } catch (FileUploadException e) {
        e.printStackTrace();
    }

    res.sendRedirect("/user/picture/upload.jsp");
}

在我的客户端,我使用JavaScript来更改文件的名称:

<script type="text/javascript">

    function uploadFile() {
        if (window.File && window.FileList) {
            var fd = new FormData();
            var files = document.getElementById('fileToUpload').files;
            for ( var i = 0; i < files.length; i++) {
                fd.append("file" + i, files[i]);
            }
            var xhr = new XMLHttpRequest();
            xhr.open("POST", document.getElementById('uploadForm').action);
            xhr.send(fd);

            alert('already saved');
            document.getElementById('uploadForm').value = '';

        } else {
            document.getElementById('uploadForm').submit(); //no html5
        }
    }

</script>

在我的html / jsp中:

<form id="uploadForm" enctype="multipart/form-data" method="post" action="<%= blobstoreService.createUploadUrl("/user/uploadPics") %>">
     <input type="file" name="fileToUpload" id="fileToUpload" multiple="multiple" size="5"/>
     <input type="button" onclick="uploadFile();" value="Upload" />
</form>

以上代码适用于开发机器,但在部署时无法正常工作。问题的可能错误和解决方案是什么?如何在appengine中看到错误?

1 个答案:

答案 0 :(得分:3)

弃用的方法getUploadedBlobs(..) does not support multiple='true'。请尝试使用getUploads(..)