我必须将文件从我的jsp传递到我的portlet动作类(java)。 我在jsp中创建了这一行..
<input type="file" name="file"/>
但是当我需要将它传递到我的portlet时我不知道如何处理它..我应该通过使用ParamUtil.getXXX()方法来处理它,但是我没有看到任何保存文件数据的东西类型..它应该看起来像这样
String tin = ParamUtil.getString(actionRequest, "tin");
String surName = ParamUtil.getString(actionRequest, "surName");
String middleName = ParamUtil.getString(actionRequest, "middleName");
String givenName = ParamUtil.getString(actionRequest, "givenName");
String nameExt = ParamUtil.getString(actionRequest, "nameExt");
File file = ParamUtil. **<----- this is where I'm stuck :(**
Date birthDate = ParamUtil.getDate(actionRequest, "birthdate", DateFormat.getInstance());
String birthPlace = ParamUtil.getString(actionRequest, "birthplace");
String citizenship = ParamUtil.getString(actionRequest, "citizenship");
String civilStatus = ParamUtil.getString(actionRequest, "civilstatus");
String fatherFName = ParamUtil.getString(actionRequest, "fgivenName");
String fatherMName = ParamUtil.getString(actionRequest, "fmiddleName");
String fatherLName = ParamUtil.getString(actionRequest, "fsurName");
String motherFName = ParamUtil.getString(actionRequest, "mgivenName");
String motherMName = ParamUtil.getString(actionRequest, "mmiddleName");
String motherLName = ParamUtil.getString(actionRequest, "msurName");
String spouseFName = ParamUtil.getString(actionRequest, "sgivenName");
String spouseMName = ParamUtil.getString(actionRequest, "smiddleName");
String spouseLName = ParamUtil.getString(actionRequest, "ssurName");
String children[] = ParamUtil.getParameterValues(actionRequest, "children[]");
long pi_id = CounterLocalServiceUtil.increment();
personal_info pi = null;
pi = new personal_infoImpl();
pi = personal_infoLocalServiceUtil.createpersonal_info((int) pi_id);
pi.setTin(tin);
pi.setLname(surName);
pi.setFname(givenName);
pi.setMname(middleName);
pi.setName_ext(nameExt);
pi.setBirthDate(birthDate);
pi.setBirthplace(birthPlace);
pi.setPhoto(file); <------------ it's supposed to be saved in here.. but I'm stuck...
由于这个原因,我甚至没有测试过代码块。
答案 0 :(得分:1)
您应该使用 UploadPortletRequest 来获取文件。
这就是我们如何处理文件列表。
JSP页面:
<form id="fileupload" method="POST" enctype="multipart/form-data" class="uploader-files pull-left">
<input type="file" name="fileUpload" multiple id="ofxUploadFile" class="file-uploader" />
....
....
</form>
控制器方法:
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
File[] uploadedFileWithExtension = uploadRequest.getFiles("fileUpload");
InputStream[] inputStream = uploadRequest.getFilesAsStream("fileUpload");
File[] inputStreamUploadedFile = new File[inputStream.length];
int i = 0;
for (InputStream fileObj : inputStream) {
if (Validator.isNull(fileObj)) {
inputStreamUploadedFile[i] = FileUtil.createTempFile();
} else {
inputStreamUploadedFile[i] = FileUtil.createTempFile(fileObj);
}
i++;
}
}