顯然該實體必須是MultiPartEntity。
這是客戶端代碼來獲取URL:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(myDomain/mayServlet);
HttpResponse httpResponse = httpClient.execute(httpPost);
Header[] headers = httpResponse.getHeaders(myHeader);
for (int i = 0; i < headers.length; i++) {
Header header = headers[i];
if(header.getName().equals(myHeader))
uploadUrl = header.getValue();
這是服務器代碼返回的網址:
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
String uploadUrl = blobstoreService.createUploadUrl(requestHandlerServlet);
resp.addHeader("uploadUrl", uploadUrl);
這是客戶端上傳代碼:
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(uploadUrl);
MultipartEntity httpEntity = new MultipartEntity();
ContentBody contentBody = new FileBody(new File("filePath/fileName"));
httpEntity.addPart("fileKey", contentBody);
httpPost.setEntity(httpEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
那麼容易...... :(
Java API和ove rview:http://code.google.com/appengine/docs/java/blobstore/overview.html – 2011-01-11 01:37:52
嗨,尼克,你是對的,我想用一個URL,我應該如何得到一個新的dinamically?調用一個servlet並在響應中返回它?有一個更好的方法? – david 2011-01-11 14:05:49