Google App Engine에서 이미지 파일을 프로그래밍 방식으로 다운로드하고 저장하고 싶습니다. Datastore API가있어 개체를 저장할 수 있습니다.Google App Engine에 파일을 쓰는 방법은 무엇입니까?
Entity e = new Entity("Kind");
e.setProperty("heigh",6);
DatastoreServiceFactory.getDatastoreService().put(e);
는하지만 내가 src=static/image1.png
처럼 HTML의 컨텍스트 내에서 참조 할 수있는 곳,에 다운로드 된 이미지 (바이트 [])로 저장해야합니다. Blobstore와 같은 소리입니다.
많은 업로드 사례를 발견했지만 업로드 양식을 원하지 않습니다. 그래서 파일 upload example처럼 POST를 컴파일하려고했지만 작동하지 않습니다. Google App Engine에서 파일을 쉽게 저장하는 솔루션이 없습니까 (무료로 제공되므로 Cloud Storage는 옵션이 아닙니다).
URL surl = new URL("http://www.freeonlinegames.com/games/39779/medium.jpg");
byte[] byteArray = URLFetchServiceFactory.getURLFetchService().fetch(surl).getContent();
StringBuilder parameters = new StringBuilder();
parameters.append("--BOUNDARY");
parameters.append("\nContent-Disposition: form-data; name=\"file\"; filename\"test.png\"\n");
parameters.append("Content-Type: image/jpg\n\n");
parameters.append(new String(byteArray));
parameters.append("\n--BOUNDARY\n");
String request = BlobstoreServiceFactory.getBlobstoreService().createUploadUrl("/upload");
URL durl = new URL(request);
log("upload: " + durl.toString());
HttpURLConnection connection = (HttpURLConnection) durl.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=--BOUNDARY");
connection.setRequestProperty("Content-Length",Integer.toString(parameters.toString().getBytes().length));
connection.setRequestProperty("Cache-Control","max-age=0");
connection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
connection.setRequestProperty("Accept-Encoding","gzip,deflate,sdch");
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(parameters.toString());
wr.flush();
wr.close();
BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
byte[] bkey = new byte[512];
bis.read(bkey);
log("resp: " + new String(bkey));
나는 같은 "해결책"을 발견했습니다. (답장을 보내 주셔서 감사합니다! –