![](/img/trans.png)
[英]how to download an uploaded blob file from google app engine using java
[英]Uploaded file not storing as blob in google app engine using java
我正在嘗試使用Apache File Upload存儲文件。 我的JSP如下所示,
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit"value="upload" />
</form>
在我的Servlet中,我可以按以下方式獲取上傳的文件,
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();
現在,我嘗試使用以下代碼將文件存儲為Blob值,
String blobKey = fileService.getBlobKey(file).getKeyString();
Entity Input = new Entity("Input");
Input.setProperty("Input File", blobKey);
datastore.put(Input);
當我嘗試此操作時,我可以存儲文件名Blob鍵,但文件未存儲。 它在Google App引擎的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();
更新:您沒有正確處理多部分-它有多個部分,您需要確保閱讀正確的部分(名稱為“文件”的部分):
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
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.