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
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
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
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd"
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
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="x"%>
storage="file"
required="true"/>
required="true">
MyResult.jsp
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
[@more@]
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/11754099/viewspace-1023746/ ,如需转载,请注明出处,否则将追究法律责任。