我能够读取字节并在系统控制台上打印出来。但是由于GAE不支持文件创建,我通过StackOverflow搜索并发现我可以写入GAE blobstore。但是我不知道如何去做,因为我是GAE的新手。
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html;charset=UTF-8");
// resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(req);
// out.println("<html><body>");
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream in = item.openStream();
if (item.isFormField()) {
out.println("<br />Got a form field: " + item.getFieldName());
} else {
out.println("<br />Got an uploaded file: " + item.getFieldName() + ", name = " + item.getName());
ZipInputStream zis = new ZipInputStream(
new BufferedInputStream( in ));
ZipEntry entry;
// Read each entry from the ZipInputStream until no
// more entry found indicated by a null return value
// of the getNextEntry() method.
byte[] buf = new byte[10244];
int len;
while ((entry = zis.getNextEntry()) != null) {
out.println("Unzipping: " + entry.getName());
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile("text/plain");
boolean lock = false;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
PrintWriter outter = new PrintWriter(Channels.newWriter(writeChannel, "UTF8"));
StringBuilder sb = new StringBuilder(buf.length);
if (entry.getName().equalsIgnoreCase("booking.csv")) {
int count = 0;
while ((len = zis.read(buf, 0, buf.length)) != -1) {
//I'm trying to write byte[] into blobstore instead of printing using
//System.out.write(buf, 0, len);
}
有什么建议吗?
答案 0 :(得分:1)
试试这个:
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
while ((len = zis.read(buf, 0, buf.length)) != -1) {
writeChannel. write(ByteBuffer.wrap(buf, 0, len), null);
}
答案 1 :(得分:0)
最简单的方法:
import com.google.appengine.api.files.FileService;
import com.google.appengine.api.files.AppEngineFile;
import com.google.appengine.api.files.FileWriteChannel;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.ServingUrlOptions;
...
// your data in byte[] format
byte[] data = image.getData();
/**
* MIME Type for
* JPG use "image/jpeg" for PNG use "image/png"
* PDF use "application/pdf"
* see more: https://en.wikipedia.org/wiki/Internet_media_type
*/
String mimeType = "image/jpeg";
// save data to Google App Engine Blobstore
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mimeType);
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
writeChannel.write(java.nio.ByteBuffer.wrap(data));
writeChannel.closeFinally();
// your blobKey to your data in Google App Engine BlobStore
BlobKey blobKey = fileService.getBlobKey(file);
// THANKS TO BLOBKEY YOU CAN GET FOR EXAMPLE SERVING URL FOR IMAGES
// Get the image serving URL (in https:// format)
String imageUrl =
ImagesServiceFactory.getImagesService().getServingUrl(
ServingUrlOptions.Builder.withBlobKey(blobKey
).secureUrl(true));