I have a working java servlet which will upload files into the bucket indicated but however, it is only able fully upload files with less than 1MB. If i were to upload a file with more then 1MB, only the 1st MB of data will be uploaded while the rest of the files will be empty.
package com.google.appengine;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.nio.ByteBuffer;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
import com.google.appengine.api.blobstore.BlobstoreInputStream;
import com.google.appengine.tools.cloudstorage.GcsFileOptions;
import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.appengine.tools.cloudstorage.GcsOutputChannel;
import com.google.appengine.tools.cloudstorage.GcsService;
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
import com.google.appengine.tools.cloudstorage.RetryParams;
public class upload extends HttpServlet {
private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String Bucketname = (String) session.getAttribute("BUCKET");
Map<String, List<BlobInfo>> blobsData = blobstoreService.getBlobInfos(request);
for (String key : blobsData.keySet())
{
for(BlobInfo blob:blobsData.get(key))
{
byte[] b = new byte[(int)blob.getSize()];
BlobstoreInputStream in = new BlobstoreInputStream(blob.getBlobKey());
in.read(b);
GcsService gcsService = GcsServiceFactory.createGcsService();
GcsFilename filename = new GcsFilename(Bucketname, blob.getFilename());
GcsFileOptions options = new GcsFileOptions.Builder()
.mimeType(blob.getContentType())
//.acl("authenticated-read")
.build();
gcsService.createOrReplace(filename,options,ByteBuffer.wrap(b));
in.close();
}
}
String SharedMessage = "File has been Uploaded Successfully!";
String SharedURL = "";
session.setAttribute("SHAREDMESSAGE",SharedMessage);
session.setAttribute("SHAREDURL",SharedURL);
response.sendRedirect("SharedResult.jsp");
}
}
Any help will be appreciated. Thank you
You're ignoring the return value of in.read(byte[]);
. It doesn't necessarily fill the whole array, especially when the array is large. You'll need to read until you get -1
, which means the stream has been exhausted.
ByteBuffer b = ByteBuffer.allocate((int)blob.getSize());
BlobstoreInputStream in = new BlobstoreInputStream(blob.getBlobKey());
byte[] buf = new byte[8192];
int bytes = 0;
while((bytes = in.read(buf)) != -1)
b.put(buf, 0, bytes);
b.flip();
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.