繁体   English   中英

为什么Google Cloud使用Google应用引擎Java存储的文件大小小于上传的文件大小

[英]Why Google Cloud Stores files less in size than the uploaded file size using Google app engine Java

将文件上传到Google云端存储时,存储文件的大小与原始上传文件不同。 存储文件的大小远小于原始大小。 我的上传servlet如下所示,

public class UploadServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    private StorageService storage = new StorageService();
    private static int BUFFER_SIZE =1024 * 1024* 10;
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    @Override
    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 is1 = 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 = is1.read(b1)) != -1) {
                            writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
                            storage.init(fileName, mime);
                            storage.storeFile(b1, readBytes1);
                        }
                        writeChannel.closeFinally();
                        String blobKey = fileService.getBlobKey(file).getKeyString();
                        Entity input = new Entity("Input");
                        input.setProperty("Input File", blobKey);
                        datastore.put(input);
                        is1.close();
                        storage.destroy();
                    } catch (Exception e) {
                        e.printStackTrace(resp.getWriter());
                    }
                }
            }
        } catch (FileUploadException e) {
            // log error here
        }
    }
}

我的存储代码如下所示,

public class StorageService {
    public static final String BUCKET_NAME = "MyBucket";
    private FileWriteChannel writeChannel = null;
    FileService fileService = FileServiceFactory.getFileService();
    private BufferedOutputStream bos = null;
    private static final Logger log = Logger.getLogger(StorageService.class.getName());
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
    public void init(String fileName, String mime) throws Exception {
        System.out.println("Storage service:init() method:  file name:"+fileName+" and mime:"+mime);
        log.info("Storage service:init() method:  file name:"+fileName+" and mime:"+mime);
        GSFileOptionsBuilder builder = new GSFileOptionsBuilder()
            .setAcl("public_read")
            .setBucket(BUCKET_NAME)
            .setKey(fileName)
            .setMimeType(mime);
        AppEngineFile writableFile = fileService.createNewGSFile(builder.build());
        boolean lock = true;
        writeChannel = fileService.openWriteChannel(writableFile, lock);
        bos = new BufferedOutputStream(Channels.newOutputStream(writeChannel));
    }
    public void storeFile(byte[] b, int readSize) throws Exception {
        bos.write(b,0,readSize);
        System.out.println(readSize);
        bos.flush();
    }
    public void destroy() throws Exception {
        log.info("Storage service: destroy() method");
        bos.close();
        writeChannel.closeFinally();
    }
    public BlobKey getBlobkey (String filename) {
        BlobKey bk = blobstoreService.createGsBlobKey("/gs/MyBucket/"+filename);
        return bk;
    }
}

当我上传一个2 MB文件的文件时,它只存储155个字节。 我不明白哪里出错了。

请建议我一个想法,

非常感谢您的帮助。

在UploadServlet类中,每个文件多次调用storage.init(...),始终重新创建文件。 这不应该发生:

while ((readBytes1 = is1.read(b1)) != -1) {
    writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
    storage.init(fileName, mime);                          // <- HERE
    storage.storeFile(b1, readBytes1);
}

您需要将init语句移出循环,在它前面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM