jsf上传
A. 如何使用jsf上传文件
使用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>
B. jsf中怎样使用使用myfaces上传文件
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}" id="" allowTypes="#.jpg;*.png;*.gif;" description="Images" update="">
受管bean:
public class FileUploadController{
public void handleFileUpload(FileUploadEvent event){
//////////////////////////
}
}
还需要添加一些Apache依赖性配置,web.xml添加一个过滤设置,以允许服务器保存状态。
C. JSF primefaces文件上传问题
你遇到的问题可能和我有点像,http://www.cnblogs.com/panlongfeng/p/7229349.html###这个是我写的博客,你看下对你有没有帮助
D. jsf 中 rich:fileUpload 控件怎样用js获得上传文件的客户端路径
首先, rich:fileUpload 是通过 fileUploadListener 来实现图片上传的,它是把上传的图片直接转换为 UploadedFile ,而这个类中没有路径这个属性,所以直接通过rich:fileUpload 应该是没法用js直接拿到路径的.
如果使用js/jquery的图片上传的方法,可能可以拿到图片路径,你可以试试
E. richface:fileupload中实现弹出提示框当上传得Excel文件错误或者数据过大时
public class FileUploadBean {
public void uploadListener(UploadEvent event) {
//获取传过来的items,3.2.2之前版本不支持getUploadItems方法
List itemList = event.getUploadItems();
for(int i=0 ; i<itemList.size() ; i++){
try {
UploadItem item = (UploadItem) itemList.get(i);
File file = new File("d:\\"+UUID.randomUUID().toString()+".jpg");
//大小不能超过4M
if(item.getFileSize() <= 4000000){
FileInputStream fis = new FileInputStream(item.getFile());
FileOutputStream out = new FileOutputStream(file);
int bytes = 0;
byte[] bteFile = new byte[1024];
while ((bytes = fis.read(bteFile)) != -1) {
out.write(bteFile, 0, bytes);
}
fis.close();
out.close();
}
} catch (Exception e) {
}
}
}
}
F. jsf怎么上传图片到mysql
要把图片保存到数据库好像不现实
上传到服务器,保存地址到数据库吧
不知道你是form有没有加上这个
<h:form id="edit" enctype="multipart/form-data"
G. jsf-impl.jar 和myfaces-impl.jar 怎么进行共存 我用JSF做了一个上传,然后用到了myfaces-impl.jar
能不能不要jsf-impl?myfaces应该实现了所有标准的jsf的API的
H. 跪求jsf中图片如何上传sqlserver2005数据库一张表的一个字段就是用来保存路径的
只要保存路径,作为字符串穿上去就可以了呗。
I. jsf怎样实现文件上传
界面
<h:form id="upLoadExcel" enctype="multipart/form-data" >
请选择Excel文件 <t:inputFileUpload id="fileupload"
value="#{ExcelListBean.upFile}"
storage="file"
maxlength="200000"/>
<f:verbatim></f:verbatim><br></br><br></br>
<h:commandButton value="提交EXCEL" action="#{ExcelListBean.addWhiteList}" styleClass="formStylebutton" type="submit" />
</h:form>
后台
导的是这个文件
import org.apache.myfaces.custom.fileupload.UploadedFile;
好像要导三个jar..
myfaces-api-1.1.5.jar
myfaces-extensions-1.0.9.jar
myfaces-impl-1.1.5.jar
javax.servlet.ServletContext sc=(javax.servlet.ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
String trace=sc.getRealPath("/")+saveName;
System.out.println(".............URL: "+trace);
FileOutputStream fileOutputStream =
new FileOutputStream(trace); //上传的文件写服务器目录
while (in.read(buffer) > 0) {
fileOutputStream.write(buffer);
}
J. jsf自定义文件上传。
Richfaces UploadFile Model.