上载的文件未使用java在google app engine中存储为blob
我正在尝试使用Apache文件上载存储文件。我的JSP如下所示上载的文件未使用java在google app engine中存储为blob,java,google-app-engine,Java,Google App Engine,我正在尝试使用Apache文件上载存储文件。我的JSP如下所示 <form action="/upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit"value="upload" /> </form> 现在我尝试使用下面的代码将文件存储为blob值 String blobKey = fil
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit"value="upload" />
</form>
现在我尝试使用下面的代码将文件存储为blob值
String blobKey = fileService.getBlobKey(file).getKeyString();
Entity Input = new Entity("Input");
Input.setProperty("Input File", blobKey);
datastore.put(Input);
当我尝试此操作时,我可以存储文件名blob key,但该文件没有存储。它在Google App engine的Blob查看器和Blob列表中显示“0”字节
请给我一个解决这个问题的办法
谢谢你的帮助
我的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());}
}
}
您正在以错误的方式从输入流读取数据。应该是:
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
while ((readBytes1 = is.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes));
}
writeChannel.closeFinally();
更新:您没有正确处理multipart-它有多个部分,您需要确保读取了正确的部分(名为“file”的部分):
您正在以错误的方式从输入流读取数据。应该是:
byte[] b1 = new byte[BUFFER_SIZE];
int readBytes1;
while ((readBytes1 = is.read(b1)) != -1) {
writeChannel.write(ByteBuffer.wrap(b1, 0, readBytes));
}
writeChannel.closeFinally();
更新:您没有正确处理multipart-它有多个部分,您需要确保读取了正确的部分(名为“file”的部分):
我试过了,但是它单独存储blob密钥,而不是文件。你能帮我吗?你用表格上传文件吗?你应该发布处理帖子的servlet的全部代码。你能检查一下吗?我已经发布了我的servlet文件。我试过了,但它只存储blob键,而不是文件。你能帮我吗?你用表格上传文件吗?你应该发布处理帖子的servlet的全部代码。现在你能检查一下吗?我已经发布了我的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();
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
}
}
}