如何使用jsf上传文件

 我来答
可以叫我表哥
推荐于2016-08-22 · 知道合伙人数码行家
可以叫我表哥
知道合伙人数码行家
采纳数:25897 获赞数:1464977
2010年毕业于北京化工大学北方学院计算机科学与技术专业毕业,学士学位,工程电子技术行业4年从业经验。

向TA提问 私信TA
展开全部
使用jsf加myfaces的upload插件实现上传文件的功能
需要的jar包:tomahawk-1.1.6.jar,jsf-impl.jar,commons-fileupload-1.2.jar,commons-io-1.3.2.jar,commons-el-1.0.jar
程序:
package model.map;
import control.MapMDelegate;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.apache.myfaces.custom.fileupload.UploadedFile;
public class FileUploadForm {
public FileUploadForm() {
}
private String name;
private UploadedFile upFile;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setUpFile(UploadedFile upFile) {
this.upFile = upFile;
}
public UploadedFile getUpFile() {
return upFile;
}

public String upload() throws IOException {
try {
InputStream in = new BufferedInputStream(upFile.getInputStream());
try {

byte[] buffer = new byte[(int)upFile.getSize()];

Date now = new Date();
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMddHHmmss");
String dt2 = sdf2.format(now);

//取得后缀名
String name = upFile.getName();
int l = name.lastIndexOf(".");
name = name.substring(l);
//生成2位随机数
Random random = new Random();
String sRand="";
String rand = null;
for (int count=0;count<2;count++){
rand=String.valueOf(random.nextInt(10));
sRand+=rand;
}

//生成在服务器端保存的文件名
String saveName = dt2 + sRand + name;
this.setName(saveName);
String trace = D:\\"+ saveName;//本地测试

FileOutputStream fileOutputStream =
new FileOutputStream(trace); //这里可以把上传的文件写服务器目录,或者数据库中 ,或者赋值给name用于文件名显示
while (in.read(buffer) > 0) {
fileOutputStream.write(buffer);
}
} catch (Exception x) {
System.out.print("Exception");
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_FATAL,
x.getClass().getName(), x.getMessage());
FacesContext.getCurrentInstance().addMessage(null, message);
return null;
}

finally {
in.close();
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getApplicationMap().put("fileupload_bytes",
upFile.getBytes());
facesContext.getExternalContext().getApplicationMap().put("fileupload_type",
upFile.getContentType());
}
System.out.println("End");
return "OK";
} catch (Exception x) {
System.out.print("Exception");
FacesMessage message =
new FacesMessage(FacesMessage.SEVERITY_FATAL,
x.getClass().getName(), x.getMessage());
FacesContext.getCurrentInstance().addMessage(null, message);
return null;
}

}
public boolean isUploaded() {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getExternalContext().getApplicationMap().get("fileupload_bytes") !=
null;
}
}

页面:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
<f:view>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030"/>
<title>add-map</title>
</head>
<body>
<h:outputText value="#{MapMDelegate.msg}" style="color:rgb(255,0,0);"/>
<h:form enctype="multipart/form-data">
<t:inputFileUpload value="#{fileuploadForm.upFile}"/>
<br/>
<h:message for="fileupload"/><h:commandButton value="上传"
action="#{fileuploadForm.upload}" />
</h:form>
<h:form>
<br/>
图纸名称 <h:inputText value="#{fileuploadForm.name}" disabled="true"/>
</h:form>
</body>
</html>
</f:view>
记得要在工程的web.xml文件中加入filter
<!--Tomahawk-->
<filter>
<filter-name>extensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<description>Set the size limit for uploaded files.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB</description>
<param-name>uploadMaxFileSize</param-name>
<param-value>10m</param-value>
</init-param>
<init-param>
<description>Set the threshold size - files
below this limit are stored in memory, files above
this limit are stored on disk.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB</description>
<param-name>uploadThresholdSize</param-name>
<param-value>1k</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>extensionsFilter</filter-name>
<url-pattern>/faces/*</url-pattern>
</filter-mapping>
还有把你的程序设置成request哦
<managed-bean>
<managed-bean-name>fileuploadForm</managed-bean-name>
<managed-bean-class>model.map.FileUploadForm</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式