2011-02-18 51 views
2

這裏是我的Blob存儲上傳命令行應用程序我的服務器端代碼:上傳到Blob存儲在谷歌應用程序引擎從一個命令行Java應用程序

public class UploadServlet extends HttpServlet 
{ 
    private static final Logger log = Logger.getLogger(UploadServlet.class.getName()); 

    private final BlobstoreService bs = BlobstoreServiceFactory.getBlobstoreService(); 

    protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException 
    { 
     final Map<String, BlobKey> blobs = bs.getUploadedBlobs(request); 
     final BlobKey blobKey = blobs.get("blob"); 

     if (blobKey == null) 
     { 
      log.severe("BlobKey was null!"); 
      response.sendRedirect("/error.html"); 
     } 
     else 
     { 
      response.sendRedirect("/image?blob-key=" + blobKey.getKeyString()); 
     } 
    } 

    /** 
    * Generates the custom single use blobstore URL's that are needed to upload to the blobstore programmatically. */ 
    protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException 
    { 
     final String uploadURL = bs.createUploadUrl("/upload"); 
     final PrintWriter pw = response.getWriter(); 
     response.setContentType("text/plain"); 
     pw.write(uploadURL); 
    } 
} 

我已經得到了下面的代碼對我的地方發展模式下工作服務器,而無需驗證代碼,所以我知道multipart/form代碼工作正常,與驗證碼,它失敗:

r = opener.open(request) 
    File "C:\Python26\lib\urllib2.py", line 397, in open 
    response = meth(req, response) 
    File "C:\Python26\lib\urllib2.py", line 510, in http_response 
    'http', request, response, code, msg, hdrs) 
    File "C:\Python26\lib\urllib2.py", line 435, in error 
    return self._call_chain(*args) 
    File "C:\Python26\lib\urllib2.py", line 369, in _call_chain 
    result = func(*args) 
    File "C:\Python26\lib\urllib2.py", line 518, in http_error_default 
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) 
HTTPError: HTTP Error 302: Found 

從Java移動到的Python命令行客戶端:

f = urllib2.urlopen('http://myapp.appspot.com/upload') 
bloburl = f.read(1024) 
print bloburl 
print 

image = file('120.jpg', 'r') 
form = MultiPartForm() 
form.add_file('blob', 'blob', image, 'image/jpeg') 

request = urllib2.Request(bloburl) 
body = str(form) 
request.add_header('Content-type', form.get_content_type()) 
request.add_header('Content-length', len(body)) 
request.add_data(body) 

opener = auth.get_auth_opener('myapp', 'username', 'password') 
r = opener.open(request) 
data = r.read() 
print data 

我只想要一個簡單的命令行工具,它需要一個文件並將其發佈到BlobStore。我無法在互聯網上的任何地方找到一個單獨的COMPLETE示例。有很多示例可以完成GAE的所有工作,但沒有一個是來自單獨客戶端的執行FORMPOST的命令行客戶端。

+0

您是否嘗試過轉儲客戶端代碼的輸出以驗證其是否包含正確的標頭? – 2011-02-21 02:35:02

回答