Reputation: 384
Am trying to Store a file with Apache File Upload. My JSP looks like below,
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit"value="upload" />
</form>
In my Servlet i can get the uploaded fie as below,
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime,fileName);
boolean lock = true;
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1 = is.read(b1, 0, BUFFER_SIZE);
while (readBytes1 != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, BUFFER_SIZE));}
writeChannel.closeFinally();
Now am trying to store the file as blob values using the below code,
String blobKey = fileService.getBlobKey(file).getKeyString();
Entity Input = new Entity("Input");
Input.setProperty("Input File", blobKey);
datastore.put(Input);
When i try this i can store the file name blob key but the file is not storing. It shows me "0" Bytes in the Blob viewer & Blob list of Google App engine.
Kindly Suggest me An idea to solve this problem,
Your help is appreciated.
My Servlet
public class UploadServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private static int BUFFER_SIZE =1024 * 1024* 10;
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter;
try {
iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String fileName = item.getName();
String mime = item.getContentType();
InputStream is = new BufferedInputStream(item.openStream());
try {
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if( !isMultipart ) {
resp.getWriter().println("File cannot be uploaded !");}
else {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime,fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
while ((readBytes1 = is.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));}
writeChannel.closeFinally();
String blobKey = fileService.getBlobKey(file).getKeyString();
Entity Input = new Entity("Input");
Input.setProperty("Input File", blobKey);
datastore.put(Input);}}
catch (Exception e) {
e.printStackTrace(resp.getWriter());}
}
}
Upvotes: 0
Views: 793
Reputation: 80340
You are reading the data from input stream in the wrong way. It should be:
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
while ((readBytes1 = is.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes));
}
writeChannel.closeFinally();
Update: you are not handling multipart correctly - it has multiple parts, you need to make sure you read the correct part (part with name "file"):
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static int BUFFER_SIZE = 1024 * 1024 * 10;
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ServletFileUpload upload = new ServletFileUpload();
boolean isMultipart = ServletFileUpload.isMultipartContent(req);
if (!isMultipart) {
resp.getWriter().println("File cannot be uploaded !");
return;
}
FileItemIterator iter;
try {
iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String fileName = item.getName();
String fieldName = item.getFieldName();
String mime = item.getContentType();
if (fieldName.equals("file")) { // the name of input field in html
InputStream is = item.openStream();
try {
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(mime, fileName);
boolean lock = true;
FileWriteChannel writeChannel = fileService.openWriteChannel(file, lock);
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
while ((readBytes1 = is.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes1));
}
writeChannel.closeFinally();
String blobKey = fileService.getBlobKey(file).getKeyString();
Entity input = new Entity("Input");
input.setProperty("Input File", blobKey);
datastore.put(input);
} catch (Exception e) {
e.printStackTrace(resp.getWriter());
}
}
}
} catch (FileUploadException e) {
// log error here
}
}
}
Upvotes: 3