简体   繁体   English

从GAE Blobstore提供文件

[英]Serve File from GAE Blobstore

I have faced this problem several times that whenever we serve files from Blobstore API. 每当我们从Blobstore API提供文件服务时,我就多次遇到此问题。 It is always downloaded as : 总是以以下方式下载:

serve.fileNameExtention serve.fileNameExtention

is there any possible way that we can serve or download the file with the name we saved it. 是否有任何可能的方式可以用来保存或保存文件名称的文件。

ie someName.docx 即someName.docx

If it is possible please tell me how to do it, because I have searched over internet but I couldn't found out any solution for it. 如果可能的话,请告诉我该怎么做,因为我已经在互联网上进行搜索,但找不到任何解决方案。

Set it in header: 将其设置在标题中:

public void doGet(HttpServletRequest req, HttpServletResponse res)
    throws IOException {
        BlobKey blobKey = new BlobKey(req.getParameter("blob-key")); //example
        String filename = "someName.docx";
        res.setHeader("Content-Disposition", "attachment; filename=\"" +fileName +\"");

        blobstoreService.serve(blobKey, res);
    }
BlobKey blobKey = new BlobKey(key);                

BlobInfo blobInfo =  new BlobInfoFactory().loadBlobInfo(blobKey);
// set response header
response.setContentType(blobInfo.getContentType());
response.setHeader("Content-Disposition", "filename=" + blobInfo.getFilename());

// serve blob
blobService.serve(blobKey, response);

You would have to change the file name in the url that you are serving the file from. 您必须在提供文件的URL中更改文件名。 You can use something like urlRewriteFilter (https://code.google.com/p/urlrewritefilter/) to rewrite the urls to allow you to still use the "serve" servlet, but change the urls on their way out of the servlet to whatever you want them to be. 您可以使用urlRewriteFilter(https://code.google.com/p/urlrewritefilter/)之类的内容来重写url,以允许您仍然使用“服务” servlet,但是将其从servlet移出时将url更改为无论您希望他们成为什么。

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

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