I want to upload a file, store it in the Blobstore and then later access it (via the BlobKey) but this won't work.
Here is my Code:
public class CsvToBlobstoreUploadServlet extends HttpServlet {
private final BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
@Override
public void doPost(final HttpServletRequest request, final HttpServletResponse res) throws ServletException, IOException {
final Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
final BlobKey blobKey = blobs.get("upload");
final BlobInfo info = new BlobInfoFactory().loadBlobInfo(blobstoreService.getUploadedBlobs(request).get("upload"));
if (blobKey == null) {
res.sendRedirect("/");
} else {
res.sendRedirect("/csvupload?blob-key=" 开发者_JAVA百科+ blobKey.getKeyString());
}
}
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(new BlobKey(req.getParameter("blob-key")));
resp.setContentType("text/html");
resp.setHeader("Content-Language", "en");
resp.getWriter().println("<blob-key>" + blobInfo.getBlobKey().getKeyString() + "</blob-key>"); // Here I get no NullPointerException, blobInfo is NOT null, everything es as expected....
}
This works! Means the File ist stored in the Blobstore, and I get something like <blob-key>jA_W_jiKoTpXAe9QjeFlrg</blob-key>
back from Post request.
Now I want to access this Blob with this key, but following Code results in NullPointerException, because blobInfo is null.... but why???
// A method from another Servlet....
private String getData(final String blobKey) {
//at this point blobKey is exactly that one returned previously for example jA_W_jiKoTpXAe9QjeFlrg
try {
final BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(new BlobKey(blobKey));
final BlobstoreInputStream bis = new BlobstoreInputStream(blobInfo.getBlobKey()); // Here I got NullPointerException, because BlobInfo is null
final InputStreamReader isr = new InputStreamReader(bis);
final BufferedReader br = new BufferedReader(isr);
final StringBuffer sb = new StringBuffer();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
return sb.toString();
} catch (final IOException e) {
e.printStackTrace();
}
return "";
}
I would be very very glad if someone could figure out what the problem is....
The following works for me it need only one helper class, called FileObject, which is a named dynamic byte buffer to append byte arrays:
public class FileObject {
private String name;
byte [] bufferArray = null;
public FileObject(String name, byte[] data) {
this.name = name;
this.bufferArray = data;
}
public FileObject(String name) {
this.name = name;
}
public void appendData(byte[] data, int numberOfBytes) {
if (bufferArray == null)
{
this.bufferArray = new byte [numberOfBytes];
System.arraycopy(data, 0, bufferArray, 0, numberOfBytes);
}
else
{
byte[] tempArray = new byte[bufferArray.length + numberOfBytes];
System.arraycopy(bufferArray, 0, tempArray, 0, bufferArray.length);
System.arraycopy(data, 0, tempArray, bufferArray.length, numb erOfBytes);
bufferArray = tempArray;
}
}
public byte[] getData() {
return bufferArray;
}
public void setData(byte[] data) {
this.bufferArray = data;
}
public String getName() {
return name;
}
}
This is the core method to write into the file object:
public synchronized static byte[] readBlob(BlobKey blobKey) throws BlobServiceException{
int bufferSize = MAX_READ_BUFFER_SIZE;
FileObject fileObject = new FileObject("");
try{
AppEngineFile file = fileService.getBlobFile(blobKey);
FileReadChannel readChannel = fileService.openReadChannel(file, false);
// write the files to the disk
ByteBuffer byteBuffer = ByteBuffer.allocate(bufferSize);
int numberOfBytes;
while ((numberOfBytes = readChannel.read(byteBuffer)) != -1) {
fileObject.appendData(byteBuffer.array(), numberOfBytes);
byteBuffer = ByteBuffer.allocate(bufferSize);
}
readChannel.close();
}catch(Exception e){
BlobServiceException blobIoException = new BlobServiceException("Failure while reading blob.\n" + e.getMessage());
blobIoException.setStackTrace(e.getStackTrace());
throw blobIoException;
}
return fileObject.getData();
}
精彩评论