我在GAE应用程序中进行gwt上传。按照建议,我实现了一个CustomUploadAction来处理DataStore中文件的存储。代码如下:
public String executeAction(HttpServletRequest request,
List<FileItem> sessionFiles) throws UploadActionException {
logger.info("Starting: DatastoreUploadAction.executeAction");
String executeAction = super.executeAction(request, sessionFiles);
for (FileItem uploadedFile : sessionFiles) {
Long entityId = new Long(2001); // This is where i wanna use a request parameter
InputStream imgStream;
try {
imgStream = uploadedFile.getInputStream();
Blob attachment = new Blob(IOUtils.toByteArray(imgStream));
String contentType = uploadedFile.getContentType();
appointmentDao.setAppointmentAttachment(entityId, attachment,
contentType);
} catch (IOException e) {
logger.error("Unable to store file", e);
throw new UploadActionException(e);
}
}
return executeAction;
}
正如您所看到的,DAO类需要"EntityID"来将上传的文件存储在DataStore中。现在我正在处理一个硬编码的值,它很好,但我希望客户端发送entityID作为请求参数。进行上传的小部件是一个MultiUploader:
private MultiUploader defaultUploader;
MultiUploader或任何其他Widget是否可以设置一个请求参数,以便我可以在UploadAction中使用它?
是的,您可以在客户端代码中设置它
有方法:MultiUploader #setServletPath(java.lang.String)
,例如:
final MultiUploader u = new MultiUploader();
...
...
...
u.setServletPath(u.getServletPath() + "?entityId="+myObject.getEntityId());
在服务器端:
String entityId= request.getParameter("entityId");
阅读本文以获取更多信息:向servlet 发送附加参数