2012-10-20 46 views
2

我正在構建一個Google App Engine應用程序,用於處理用戶輸入文件,將其模型存儲在數據存儲區中,並提供用戶以任何格式下載文件在處理模型後,爲此我需要爲我的處理文件創建一個下載鏈接。如何使用Java從Google App Engine創建下載鏈接

我在Python中發現了一些東西,但我對它的工作原理並不熟悉。

我需要做的是在Java中。任何幫助表示讚賞。覆蓋它的代碼非常棒。

+0

你保存到數據存儲作爲一個'Blob'財產或Blobstore? –

+0

不,它保存爲一個對象,處理該對象以獲取要寫入可下載文件的輸出。 – Daren

回答

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()); 
     } 
    } 
} 
相關問題