我已经尝试了几个小时,所以也许一双新鲜的眼睛会有所帮助。
我正在尝试使用Google App Engine上的GWT(使用UiBinder)将文件上传到服务器。按照我可以找到的所有示例,看起来就像它们应该工作一样,但服务器的'post'方法显示上传了0个项目。
这是客户端代码:
FormUploader.ui.xml:
<g:FormPanel action="/formImageUploader" ui:field="formPanel">
<g:VerticalPanel>
<g:FileUpload ui:field="uploader"></g:FileUpload>
<g:Button ui:field="submit">Submit</g:Button>
</g:VerticalPanel>
</g:FormPanel>
FormUploader.java:
@UiField
FormPanel formPanel;
@UiField
FileUpload uploader;
@UiField
Button submit;
public FormUploader() {
initWidget(uiBinder.createAndBindUi(this));
formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
formPanel.setMethod(FormPanel.METHOD_POST);
addSubmitHandlers();
}
private void addSubmitHandlers() {
formPanel.addSubmitCompleteHandler(new SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
Core.log("Inside submitComplete");
Window.alert(event.getResults());
}
});
}
的web.xml:
<servlet>
<servlet-name>formImageUploader</servlet-name>
<servlet-class>com.company.server.FormImageUploader</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>formImageUploader</servlet-name>
<url-pattern>/formImageUploader</url-pattern>
</servlet-mapping>
FormImageUploader.java:
public class FormImageUploader extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
log.info("Request.getContentLength(): " + request.getContentLength());
log.info("Content type: " + request.getContentType());
log.info("Parm names: ");
for (Object paramName : request.getParameterMap().keySet()) {
log.info("__Param name: " + paramName.toString());
}
log.info("Attribtue names: ");
Enumeration enu = request.getAttributeNames();
while (enu.hasMoreElements()) {
log.info("__Attribute name: " + enu.nextElement().toString());
}
log.info("Header names: ");
enu = request.getHeaderNames();
while (enu.hasMoreElements()) {
log.info("__Header name: " + enu.nextElement().toString());
}
resp.getWriter().println("Post Code has finished executing");
}
所有这些都执行得很好,客户端显示一个警告框,其中包含邮政编码已完成执行,但我无法弄清楚如何获取我要上传的文件的内容。
这是服务器端日志:
Request.getContentLength(): 63
Content type: multipart/form-data; boundary=---------------------------194272772210834546661
Parm names:
Attribtue names:
__Attribute name: com.google.apphosting.runtime.jetty.APP_VERSION_REQUEST_ATTR
任何人都可以帮我看看为什么我在客户端点击“提交”后没有在服务器上看到任何文件?
提前致谢
修改
仍然无法弄清楚为什么我无法在服务器上获取表单数据。我正在关注this example,只是尝试将响应写入客户端,但服务器肯定没有看到客户端的任何内容。
服务器代码:
try {
log.info("Inside try block");
ServletFileUpload upload = new ServletFileUpload();
log.info("created the servlet file upload");
res.setContentType("text/plain");
log.info("set the content type to text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
log.info("Got the iterator for items");
while (iterator.hasNext()) {
log.info("__inside iterator.hasNext");
FileItemStream item = iterator.next();
log.info("__assigned the file item stream");
InputStream stream = item.openStream();
log.info("__opened said stream");
if (item.isFormField()) {
log.warning("Got a form field: " + item.getFieldName());
} else {
log.warning("Got an uploaded file: " + item.getFieldName() +
", name = " + item.getName());
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
res.getOutputStream().write(buffer, 0, len);
}
log.info("Done writing the input to the output stream");
}
}
} catch (Exception ex) {
log.severe("Exception; " + ex);
throw new ServletException(ex);
}
log.info("Done parseInput3");
日志:
Inside parseInput3
Inside try block
created the servlet file upload
set the content type to text/plain
Got the iterator for items
Done parseInput3
同样,它绝对无法迭代服务器上的文件项......任何想法为什么?
答案 0 :(得分:2)
事实证明,GAE有一种完全不同的上传文件的方式(Blob): http://code.google.com/appengine/docs/java/blobstore/overview.html#Uploading_a_Blob
您需要在客户端上提供来自服务器的Blobstore上传URL(超时),并使用以下方式检索:
blobstoreService.createUploadUrl('urlToRedirectToAfterUploadComplete')
在表单操作上设置上传URL后,表单将被提交,blobstore服务将重定向到提供的URL,然后可以访问Google存储的对象的BlobKey
。
因此,我将以下ChangeHandler
添加到我的GWT FileUpload
:
private void addChangeListener() {
Core.log("Selected file: " + uploader.getFilename());
uploader.addChangeHandler(new ChangeHandler() {
@Override
public void onChange(ChangeEvent event) {
MyApp.SERVICES.getUploadUrl(new SuccessAsyncCallback<String>() {
@Override
public void onSuccess(String uploadUrl) {
form.setAction(uploadUrl);
form.submit();
Core.log("Submitted form with upload url: " + uploadUrl);
}
});
}
});
}
然后在servlet中我将GAE重定向到上传后:
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws Exception {
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
// where 'uploader' is the name of the FileUploader (<input type='file'>) control on the client
List<BlobKey> blobKeys = blobs.get("uploader");
resp.getOutputStream().print(blobKeys.get(0).getKeyString());
}
注意!
如果FileUpload
控件没有定义name="whateveryouwantthenametobe"
属性,GAE会在尝试获取上传的blob值时抛出OutOfMemory异常,即使你告诉它(使用UiBinder)id是ui:field="nameofControl"
答案 1 :(得分:0)
您无法写入本地文件系统 - 您应该查看files api。