2

我正在构建一个 Google App Engine 应用程序,它处理用户输入文件,将其模型存储在数据存储中,并在处理模型后让用户以任何格式下载文件,为此我需要为我的处理过的文件。

我在 Python 中找到了一些东西,但我不熟悉它的工作原理。

我需要用Java来做。任何帮助表示赞赏。涵盖它的代码很棒。

4

1 回答 1

3

这是jsp文件

<%@ page import="com.google.appengine.api.blobstore.BlobstoreServiceFactory" %>
<%@ page import="com.google.appengine.api.blobstore.BlobstoreService" %>

<%
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
%>


<html>
<head>
    <%@ page
            language="java"
            contentType="text/html; charset=UTF-8"
            pageEncoding="UTF-8"
            %>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Upload Test</title>
</head>
<body>
<form action="<%= blobstoreService.createUploadUrl("/upload") %>" method="post" enctype="multipart/form-data">
<table>
    <tr>
        <p>Bir seferde maksimum 20 dosya yükleyebilirsiniz.</p>
    </tr>
    <tr>
        <td><input type="file" name="myFile" multiple="multiple" size="20" style="width: 522px;"></td>
    </tr>
    <tr>
        <td>
            <input type="submit" value="Yükle">
        </td>
    </tr>
</table>
</form>
</body>
</html> 

这是对应的servlet

public class UploadDocument extends HttpServlet {
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

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

        Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
        List<BlobKey> blobKeys = blobs.get("myFile");

        if (blobKeys == null) {
            res.sendRedirect("/");
        } else {

            res.sendRedirect("/serve?blob-key=" + blobKeys.get(0).getKeyString());
        }
    }


}

这是下载 servlet

public class ShowImage extends HttpServlet {
    private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();

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

    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        String par = req.getParameter("name");
        if (par != null) {
            Query query = new Query("__BlobInfo__");
            query.addFilter("filename", Query.FilterOperator.EQUAL, req.getParameter("name"));

            DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
            PreparedQuery pq = datastore.prepare(query);
            List<Entity> entList = pq.asList(FetchOptions.Builder.withLimit(1));
            if (entList.size() > 0) {
                BlobKey blobKey = new BlobKey(entList.get(0).getKey().getName());
                BlobInfoFactory bi = new BlobInfoFactory();
                String fname = bi.loadBlobInfo(blobKey).getFilename();
                if (fname.contains(".jpg") || fname.contains(".JPG") || fname.contains(".jpeg") ||
                        fname.contains(".JPEG") || fname.contains(".png") || fname.contains(".PNG") ||
                        fname.contains(".GIF") || fname.contains(".gif") || fname.contains(".BMP") ||
                        fname.contains(".bmp")) {
                    res.setContentType("application/octet-stream");
                    res.setHeader("Content-Type", "save as filename=" + fname);
                    ImagesService imagesService = ImagesServiceFactory.getImagesService();

                    Image oldImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
                    Transform resize = ImagesServiceFactory.makeResize(580, 270,true);

                    Image newImage = imagesService.applyTransform(resize, oldImage);

                    byte[] newImageData = newImage.getImageData();
                    OutputStream outputStream = res.getOutputStream();
                    outputStream.write(newImageData);
                } else {
                    res.setContentType("application/x-download");
                    res.setHeader("Content-Disposition", "attachment; filename=" + fname);
                    blobstoreService.serve(blobKey, res);
                }


            } else {
                res.setContentType("text/plain");
                res.setCharacterEncoding("UTF-8");
                res.getOutputStream().write("Bu isimde bir dosya bulunamadı".getBytes());
            }
        } else {

            res.setContentType("text/plain");
            res.setCharacterEncoding("UTF-8");
            res.getOutputStream().write("Lütfen parametre giriniz. Örnek: name=resim.jpg".getBytes());
        }
    }
}
于 2012-10-20T16:37:36.080 回答