我在GAE应用程序中使用gwt-upload。作为suggested,我实现了一个Custom UploadAction来处理文件在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-或任何其他小部件-是否可以设置请求参数,以便我可以在自己的UploadAction中使用它?
最佳答案
是的,您可以在客户端代码上进行设置。有方法:MultiUploader #setServletPath(java.lang.String)
,例如:
final MultiUploader u = new MultiUploader();
...
...
...
u.setServletPath(u.getServletPath() + "?entityId="+myObject.getEntityId());
在服务器端:
String entityId= request.getParameter("entityId");
阅读此以获取更多信息:Sending additional parameters to the servlet