JSF的Sun 1.2實現增加Apache tomahawk實現檔案上傳
JSF的Sun 1.2實現增加Apache tomahawk實現檔案上傳
(1)啟動Myeclipse 6.5,建立新工程,增加JSF支援、Spring支援、Hibernate支援。
(2)把tomahawk需要的包tomahawk12-1.1.9.jar commons-fileupload-1.2.1.jar commons-io-1.4.jar commons-logging-1.0.4.jar
複製到工程的WebRootWEB-INFlib目錄下
(3)配置web.xml
xsi:schemaLocation=" "
version="2.4">
<!-- Richfaces begin 0000000000000000000000000000000 -->
<!-- Plugging the "Blue Sky" skin into the project -->
<!-- Making the RichFaces skin spread to standard HTML controls -->
<!-- Defining and mapping the RichFaces filter -->
<!-- RichFaces end00000000000000000000000000000000000 -->
<!-- Extensions Filter 111111111111111111111111111111111111-->
org.apache.myfaces.component.html.util.ExtensionsFilter
Set the size limit for uploaded files. Format: 10 - 10
bytes 10k - 10 KB 10m - 10 MB 1g - 1 GB
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
<!--
-->
<!-- Extensions Filter 111111111111111111111111111111111111-->
Enterprise supplementary medical insurance Application
<!--
- Location of the XML file that defines the root application context
- Applied by ContextLoaderListener.
-->
/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-security.xml
org.springframework.web.filter.DelegatingFilterProxy
<!--
- Loads the root application context of this web app at startup.
- The application context is then available via
- WebApplicationContextUtils.getWebApplicationContext(servletContext).
-->
org.springframework.web.context.ContextLoaderListener
<!--
- Publishes events for session creation and destruction through the application
- context. Optional unless concurrent session control is being used.
-->
org.springframework.security.ui.session.HttpSessionEventPublisher
org.springframework.web.util.Log4jConfigListener
<!--
- Provides core MVC application controller. See contacts-servlet.xml.
-->
<!--
-->
<!-- 異常的配置 -->
<!-- 異常的配置 -->
(4)配置faces-config.xml
xsi:schemaLocation=" "
version="1.2">
(5) java類
package ora.eastwill.jsfupload;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.*;
public class MyBean {
private UploadedFile myFile;
private String myParam;
private String myResult;
//建立日誌物件
Log log=LogFactory.getLog(this.getClass());
public UploadedFile getMyFile() {
return myFile;
}
public void setMyFile(UploadedFile myFile) {
this.myFile = myFile;
}
public String getMyParam() {
return myParam;
}
public void setMyParam(String myParam) {
this.myParam = myParam;
}
public String getMyResult() {
return myResult;
}
public void setMyResult(String myResult) {
this.myResult = myResult;
}
public String processMyFile() {
log.info("begin processMyFile");
System.out.print("begin start");
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();
log.info("success myResult="+myResult);
File file = new File("d:123.txt");
// 大小不能超過10M
if (myFile.getSize() <= 10000000) {
//FileInputStream fis = new FileInputStream(myFile);
InputStream fis = myFile.getInputStream();
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();
}
return "OK";
} catch (Exception x) {
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_FATAL,
x.getClass().getName(), x.getMessage());
FacesContext.getCurrentInstance().addMessage(
null, message);
return null;
}
}
}
(6)Web頁面有兩個:
MyForm.jsp
storage="file"
required="true"/>
required="true">
MyResult.jsp
[@more@]
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11754099/viewspace-1023746/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- ajax實現檔案上傳
- AngularJS實現的檔案檔案上傳AngularJS
- PHP實現單檔案、多檔案上傳 封裝 物件導向實現檔案上傳PHP封裝物件
- 使用java的MultipartFile實現layui官網檔案上傳實現全部示例,java檔案上傳JavaUI
- HttpFileCollection 實現多檔案上傳HTTP
- 檔案上傳原理和實現
- springmvc實現檔案上傳SpringMVC
- js實現帶上傳進度的檔案上傳JS
- Web上傳檔案的原理及實現Web
- PHP實現圖片(檔案)上傳PHP
- Java檔案上傳如何實現呢?Java
- 關於node實現檔案上傳
- 使用Spring實現上傳檔案Spring
- Spring mvc檔案上傳實現SpringMVC
- JS實現檔案自動上傳JS
- 通過配置檔案(.htaccess)實現檔案上傳
- java實現sftp檔案的上傳下載JavaFTP
- Jsp+Servlet實現檔案上傳下載(一)--檔案上傳JSServlet
- struts動態多檔案上傳實現
- 【node】檔案上傳功能簡易實現
- 自定義檔案上傳功能實現方法
- SpringMVC多個檔案上傳實現SpringMVC
- Feign實現檔案上傳下載
- node中間層實現檔案上傳
- Windows Phone7 實現檔案上傳Windows
- Spring Cloud Feign的檔案上傳實現SpringCloud
- vue實現Excel檔案的上傳與下載VueExcel
- 利用FormData物件實現AJAX檔案上傳功能及後端實現ORM物件後端
- python selenium +autoit實現檔案上傳 --實踐Python
- 前端實現檔案下載和拖拽上傳前端
- JavaScript+PHP實現影片檔案分片上傳JavaScriptPHP
- SpringMVC實現多檔案上傳原始碼SpringMVC原始碼
- SpringMVC實現檔案上傳&下載(2)SpringMVC
- 使用Spring Boot實現檔案上傳功能Spring Boot
- 用PHP實現上傳的ZIP檔案的解壓PHP
- JAVA實現大檔案分片上傳斷點續傳Java斷點
- struts1.2 上傳檔案
- 應用koa的上傳檔案,手動實現koa