Фото загружено некорректно GCS App Engine
Я работаю над своим сервлетом upload.java для хранения .jpg в GCS.
Я использую клиентскую библиотеку GCS вместо Google Cloud Storage API. Но я не могу найти образец для загрузки фотографии (метод doPost), поэтому мой код не работает ...
Отправка прошла успешно (я вижу свою фотографию в GCS с хорошим размером 465,24 КБ), но когда я пытаюсь просмотреть эту фотографию, я не могу:
Вот мой код:
сервлет upload.java public void doPost (запрос HttpServletRequest, ответ HttpServletResponse) выдает IOException {
//Read the file contents from the input stream
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename filename = new GcsFilename(BUCKETNAME, FILENAME);
GcsFileOptions options = new GcsFileOptions.Builder()
.mimeType("image/jpeg")
.acl("public-read")
.addUserMetadata("myfield1", "my field value")
.build();
GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options);
InputStream is = request.getInputStream();
try {
copy(is, Channels.newOutputStream(writeChannel));
} finally {
writeChannel.close();
is.close();
}
}
private void copy(InputStream input, OutputStream output) throws IOException {
byte[] buffer = new byte[256];
int bytesRead = input.read(buffer);
while (bytesRead != -1) {
output.write(buffer, 0, bytesRead);
bytesRead = input.read(buffer);
}
}
Код Android (Отправить фото)
HttpPost httppost = new HttpPost("adress.appengine.com/upload");
//On créé un fichier File qui contient la photo que l'on vient de prendre (Accessible via son Path)
File f = new File(AccueilActivity.fileUri.getPath());
//On créé un FileBody (Contenu de notre requête http POST) contenant notre photo
FileBody fileBody = new FileBody(f);
//Permet de créer une requête avec plusieurs partie
MultipartEntity reqEntity = new MultipartEntity();
//On ajoute notre photo avec le mot clé file.
reqEntity.addPart("file", fileBody);
//On set notre requête HTTP
httppost.setEntity(reqEntity);
//On execute notre requete HTTP Post, et on appele du coup automatiquement le servlet "uploaded",
//et on met la réponse dans response.
HttpResponse response = httpClient.execute(httppost);
//Ici on récupère l'entête de notre réponse HTTP (Tout sauf le code réponse)
HttpEntity urlEntity = response.getEntity();
Заранее спасибо !