如何使用jsf上传文件

2025-04-29 00:36:30
推荐回答(1个)
回答1:

使用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;
}
}

页面:
"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"%>




add-map







action="#{fileuploadForm.upload}" />




图纸名称




记得要在工程的web.xml文件中加入filter


extensionsFilter
org.apache.myfaces.webapp.filter.ExtensionsFilter

Set the size limit for uploaded files.
Format: 10 - 10 bytes
10k - 10 KB
10m - 10 MB
1g - 1 GB

uploadMaxFileSize
10m


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

uploadThresholdSize
1k



extensionsFilter
/faces/*

还有把你的程序设置成request哦

fileuploadForm
model.map.FileUploadForm
request