gpt4 book ai didi

java - 谷歌应用引擎 : NullpointerException while storing image in Blobstore through a java servlet

转载 作者:行者123 更新时间:2023-11-29 08:13:54 24 4
gpt4 key购买 nike

我正在尝试通过 JSP 文件上传和验证 servlet 将图像上传到 Blobstore。 JSP部分如下:

<form action="/testuploadmimevalidation?provider-key=testprovider" method="post" enctype="multipart/form-data">
<input type="text" name="foo">
<input type="file" name="myfile" >
<input type="submit" value="Submit">
</form>

java类TestUploadMimeValidation如下:

public class TestUploadMimeValidation extends HttpServlet {

private BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
private static final Logger log = Logger.getLogger(TestUploadMimeValidation.class.getName());
private static final boolean PRODUCTION_MODE = SystemProperty.environment.value() == SystemProperty.Environment.Value.Production;
private static final String URL_PREFIX = PRODUCTION_MODE ? "" : "http://127.0.0.1:8080";

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
InputStream in = req.getInputStream();
int formDataLength = req.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();

while ((len = in.read(dataBytes, 0, formDataLength)) != -1)
bos.write(dataBytes, 0, len);

dataBytes = bos.toByteArray();
String urlStr = URL_PREFIX + BlobstoreServiceFactory.getBlobstoreService().createUploadUrl("/testupload");
URLFetchService urlFetch = URLFetchServiceFactory.getURLFetchService();
HTTPRequest request = new HTTPRequest(new URL(urlStr), HTTPMethod.POST, FetchOptions.Builder.withDeadline(20.0));
request.setHeader(new HTTPHeader("Content-Type", "multipart/form-data"));
request.setPayload(dataBytes);

System.out.println("step1");

try {
HTTPResponse response = urlFetch.fetch(request);
System.out.println("step2");
} catch (IOException e) {

} catch (NullPointerException e) {

}

System.out.println("step3");
}
}

这个类将图像上传到 Blobstore 很好,但我得到了一个 NullPointerException。堆栈跟踪如下:

WARNING: /_ah/upload/ag5tOGJ5dXMtZGV2ZWxvcHIbCxIVX19CbG9iVXBsb2FkU2Vzc2lvbl9fGAIM
java.lang.NullPointerException
at javax.mail.internet.MimeMultipart.writeTo(MimeMultipart.java:143)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.handleUpload(UploadBlobServlet.java:180)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.access$000(UploadBlobServlet.java:72)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet$1.run(UploadBlobServlet.java:101)
at java.security.AccessController.doPrivileged(Native Method)
at com.google.appengine.api.blobstore.dev.UploadBlobServlet.doPost(UploadBlobServlet.java:98)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:58)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:122)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.apphosting.utils.jetty.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:70)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:351)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

由于这个 NPE,我在 TestUpload.java 类中的代码没有得到执行。 println 输出正常。它打印“step3”。图像被存储到数据存储中。但是我无法让 TestUpload 中的代码运行。您知道可能导致异常的原因吗?我试图弄乱多部分,但不是很成功。

如果您能就此问题提供任何帮助,我们将不胜感激。

最佳答案

我用

<form action="<%= blobstoreService.createUploadUrl("/catalog/actions/add") %>" method="post" enctype="multipart/form-data" >

关于java - 谷歌应用引擎 : NullpointerException while storing image in Blobstore through a java servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6034743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com