JSF的上传文问题
请大侠帮忙:在网上搜到一个JSF上传的例子,但不明白怎样设置文件保存的路径
上传的MyBean:
public class MyBean {
private UploadedFile myFile;
private String myParam;
private String myResult;
Upload upload=new Upload();
public String processMyFile() {
try {
MessageDigest md = MessageDigest.getInstance(myParam);
InputStream in = new BufferedInputStream(myFile.getInputStream());
try {
byte[] buffer = new byte[64 * 1024];
int count;
while ((count = in.read(buffer)) > 0)
md.update(buffer, 0, count);
} finally {
in.close();
}
byte hash[] = md.digest();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
int b = hash[i] & 0xFF;
int c = (b >> 4) & 0xF;
c = c < 10 ? '0' + c : 'A' + c - 10;
buf.append((char) c);
c = b & 0xF;
c = c < 10 ? '0' + c : 'A' + c - 10;
buf.append((char) c);
}
myResult = buf.toString();
return "OK";
} catch (Exception x) {
x.printStackTrace();
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_FATAL, x.getClass().getName(), x
.getMessage());
FacesContext.getCurrentInstance().addMessage(null, message);
return null;
}
}
这是上传的MyForm.jsp:
<f:view>
<h:form id="MyForm" enctype="multipart/form-data">
<h:messages globalOnly="true" styleClass="message" />
<h:panelGrid columns="3" border="0" cellspacing="5">
<h:outputLabel for="myFileId" value="File: " />
<x:inputFileUpload id="myFileId" value="#{uploadBean.myFile}"
storage="file" required="true" />
<h:message for="myFileId" />
<h:outputLabel for="myParamId" value="Param: " />
<h:selectOneMenu id="myParamId" value="#{uploadBean.myParam}"
required="true">
<f:selectItem itemLabel="" itemValue="" />
<f:selectItem itemLabel="MD5" itemValue="MD5" />
<f:selectItem itemLabel="SHA-1" itemValue="SHA-1" />
<f:selectItem itemLabel="SHA-256" itemValue="SHA-256" />
<f:selectItem itemLabel="SHA-384" itemValue="SHA-384" />
<f:selectItem itemLabel="SHA-512" itemValue="SHA-512" />
</h:selectOneMenu>
<h:message for="myParamId" />
<h:outputText value=" " />
<h:commandButton value="Submit" action="#{uploadBean.processMyFile}" />
<h:outputText value=" " />
</h:panelGrid>
</h:form>
</f:view>
------解决方案--------------------Java code
String name = uploadedFile.getName();
String realPath = ((ServletContext) FacesContext.getCurrentInstance()
.getExternalContext().getContext()).getRealPath("/");