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.