Java 照片don';t正确上传GCS应用程序引擎
我正在使用upload.java servlet将.jpg存储到GCS 我使用GCS客户端库而不是Google云存储API。但是我没有找到任何上传照片的示例(doPost方法),所以我的代码不起作用 发送是成功的(我可以在地面军事系统上看到大小为465.24KB的照片),但当我试图查看这张照片时,我无法: 这是我的密码: upload.java servlet public void doPost(HttpServletRequest请求、HttpServletResponse响应) 抛出IOException{Java 照片don';t正确上传GCS应用程序引擎,java,google-app-engine,rest,google-cloud-storage,Java,Google App Engine,Rest,Google Cloud Storage,我正在使用upload.java servlet将.jpg存储到GCS 我使用GCS客户端库而不是Google云存储API。但是我没有找到任何上传照片的示例(doPost方法),所以我的代码不起作用 发送是成功的(我可以在地面军事系统上看到大小为465.24KB的照片),但当我试图查看这张照片时,我无法: 这是我的密码: upload.java servlet public void doPost(HttpServletRequest请求、HttpServletResponse响应) 抛出IO
//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代码(发送照片)
提前感谢!我终于找到了解决我问题的方法!我为此感到非常自豪
Android的代码:
//Actions à réaliser en fond
protected String doInBackground(String... params) {
//Création d'une requête HTTP
HttpClient httpClient = new DefaultHttpClient();
//On construit notre adresse Post grâce à ce que l'on vient de récupérer
HttpPost httppost = new HttpPost("http://your_id.appspot.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());
//Et là on essaie !
try {
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
entityBuilder.addBinaryBody("Photo_a_uploader", f);
entityBuilder.addTextBody("Type", "Type que l'utilisateur a choisi");
httppost.setEntity(entityBuilder.build());
HttpResponse response = httpClient.execute(httppost);
} catch (ClientProtocolException e) {
// Si on tombe dans le catch, on set notre variable à 2.
e.printStackTrace();
} catch (IOException e) {
// Si on tombe dans le catch, on set notre variable à 2.
e.printStackTrace();
}
代码应用程序引擎
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, 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/jpg")
.acl("public-read")
.addUserMetadata("myfield1", "my field value")
.build();
GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options);
ServletFileUpload upload = new ServletFileUpload();
res.setContentType("text/plain");
try {
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
log.warning("Champs texte avec id: " + item.getFieldName()+", et nom: "+Streams.asString(stream));
} else {
log.warning("Nous avons un fichier à uploader : " + item.getFieldName() +
", appelé = " + item.getName());
// You now have the filename (item.getName() and the
// contents (which you can read from stream). Here we just
// print them back out to the servlet output stream, but you
// will probably want to do something more interesting (for
// example, wrap them in a Blob and commit them to the
// datastore).
// Open a channel to write to it
byte[] bytes = ByteStreams.toByteArray(stream);
try {
writeChannel.write(ByteBuffer.wrap(bytes));
} finally {
writeChannel.close();
stream.close();
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我希望这些示例可以帮助您使用app engine和google云存储!FYI-我包括了这些导入来支持您的解决方案:
import com.google.appengine.repackage.com.google.api.client.util.ByteStreams;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.commons.fileupload.fileiterator;import org。apache.commons.fileupload.FileItemStream;import org.apache.commons.fileupload.FileUploadException;
@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, 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/jpg")
.acl("public-read")
.addUserMetadata("myfield1", "my field value")
.build();
GcsOutputChannel writeChannel = gcsService.createOrReplace(filename, options);
ServletFileUpload upload = new ServletFileUpload();
res.setContentType("text/plain");
try {
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
log.warning("Champs texte avec id: " + item.getFieldName()+", et nom: "+Streams.asString(stream));
} else {
log.warning("Nous avons un fichier à uploader : " + item.getFieldName() +
", appelé = " + item.getName());
// You now have the filename (item.getName() and the
// contents (which you can read from stream). Here we just
// print them back out to the servlet output stream, but you
// will probably want to do something more interesting (for
// example, wrap them in a Blob and commit them to the
// datastore).
// Open a channel to write to it
byte[] bytes = ByteStreams.toByteArray(stream);
try {
writeChannel.write(ByteBuffer.wrap(bytes));
} finally {
writeChannel.close();
stream.close();
}
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}