2013-02-02 44 views
0

我正在創建一個需要圖片上傳功能的應用程序。我一直在遵循this教程和App Engine DocumentationGAE和GWT - response.sendRedirect不重定向

圖像上傳正確,服務器重定向到FileUpload HttpServlet的doPost函數。我可以從請求中讀取blob密鑰並將其保存在數據存儲中。

我的問題是發回一個響應給客戶端。我見過的所有東西都指向使用response.sendRedirect函數,但尚未成功。

public class FileUpload extends HttpServlet 
{ 
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{ 
    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 

    Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(request); 

    BlobKey blobKey = blobs.get("picFileUpload").get(0); 

    ShipHull shipHull = new ShipHull(); 
    shipHull.setShipHullName(request.getParameter("hullNameText")); 
    shipHull.setShipImageURL("/shipbuilder/blobService?blob-key=" + blobKey.getKeyString()); 

    PersistenceManager pm = PMF.get().getPersistenceManager(); 

    try 
    { 
     pm.makePersistent(shipHull); 
    } 
    catch (Exception e) 
    { 
     String hi = "hello"; 
    } 
    finally 
    { 
     pm.close(); 
    } 

    Boolean test = response.isCommitted(); 

    response.sendRedirect("/shipbuilder/FileUpload?gwt.codesvr=127.0.0.1:9997&shipHullName=" + shipHull.getShipHullName()); 

    test = response.isCommitted(); 

    return; 
} 

@Override 
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException 
{ 
    String id = req.getParameter("shipHullName"); 
    resp.setHeader("Content-Type", "text/html"); 
    resp.getWriter().println(id); 
} 

} 

我正試圖將客戶端重定向到這個servlet中的doGet。我已經嘗試過,沒有gwt.codesvr=127.0.0.1:9997shipHullName=" + shipHull.getShipHullName()),但doGet函數永遠不會到達。我也試過https://www.google.com

這是在開發服務器上完成的所有事情(尚未在生產服務器上嘗試過)。

如果您有任何其他方法返回圖像保存的狀態(如已經採取了文件名),我不介意嘗試不同的東西。

謝謝!

+0

你有任何運氣與建議? – SSR

回答

0

你可以試着把一個成功/失敗的字符串放入resp對象。

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{ 
try{ 
    processFileUploads(req) 
    resp.getWriter().print("Success"); 
}{ 
catch(Exception e){ 
    resp.getWriter().println("Unable to upload the file - Upload Failed"); 
} 
} 
+0

對不起,我對此反應遲緩,但是的,我已經嘗試過了,當我使用writer時,客戶端仍然看到空的響應,我感覺這是blobstore的一些特殊行爲,因爲它從另一個服務器進程轉發到這個post servlet。 我會在下一個機會發布客戶端代碼。也許這可能會讓我們有所瞭解。 –

0

我已經找到了問題所在。我想我的問題與this post相同。

我不得不點擊GWT開發模式工具箱圖標並添加網絡服務器「ammo-box」(我的電腦名稱)和代碼服務器爲「127.0.0.1」。當我將瀏覽器指向該開發鏈接時,它的所有工作,甚至你給SSR的答案。這是一個域的切換問題。

感謝您的幫助。

+0

您可以通過選擇您的答案來關閉查詢。如果你覺得其他答案幫助upvote :)。保持stackoverflow乾淨。 – SSR