当前位置: 首页 > 工具软件 > Tomahawk > 使用案例 >

JSF的Sun 1.2实现增加Apache tomahawk实现文件上传

戚建华
2023-12-01

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
<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/j2ee"
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 --&gt
<!-- Plugging the "Blue Sky" skin into the project --&gt

org.richfaces.SKIN
blueSky

<!-- Making the RichFaces skin spread to standard HTML controls --&gt

org.richfaces.CONTROL_SKINNING
enable


<!-- Defining and mapping the RichFaces filter --&gt

RichFaces Filter
richfaces
org.ajax4jsf.Filter



richfaces
Faces Servlet
REQUEST
FORWARD
INCLUDE

<!-- RichFaces end00000000000000000000000000000000000 --&gt

<!-- Extensions Filter 111111111111111111111111111111111111--&gt

extensionsFilter

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

uploadMaxFileSize
100m



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
100k

<!--

uploadRepositoryPath
/temp
Set the path where the intermediary files will be stored.


--&gt


extensionsFilter
*.faces


extensionsFilter
/faces/*


<!-- Extensions Filter 111111111111111111111111111111111111--&gt


Enterprise supplementary medical insurance Application

<!--
- Location of the XML file that defines the root application context
- Applied by ContextLoaderListener.
--&gt

javax.faces.CONFIG_FILES
/WEB-INF/faces-config.xml


contextConfigLocation

/WEB-INF/applicationContext.xml
/WEB-INF/applicationContext-security.xml


log4jConfigLocation
/WEB-INF/log4j.properties


springSecurityFilterChain

org.springframework.web.filter.DelegatingFilterProxy


springSecurityFilterChain
/*


<!--
- Loads the root application context of this web app at startup.
- The application context is then available via
- WebApplicationContextUtils.getWebApplicationContext(servletContext).
--&gt


org.springframework.web.context.ContextLoaderListener

<!--
- Publishes events for session creation and destruction through the application
- context. Optional unless concurrent session control is being used.
--&gt


org.springframework.security.ui.session.HttpSessionEventPublisher



org.springframework.web.util.Log4jConfigListener

<!--
- Provides core MVC application controller. See contacts-servlet.xml.
--&gt
<!--
bank
org.springframework.web.servlet.DispatcherServlet
1



bank
*.html

--&gt

Faces Servlet
javax.faces.webapp.FacesServlet
0


This is the description of my J2EE component
This is the display name of my J2EE component
list
list


This is the description of my J2EE component
This is the display name of my J2EE component
getipinfo
org.eastwill.model.service.getipinfo



Faces Servlet
*.faces


/index.jsp


<!-- 异常的配置 --&gt

<!-- 异常的配置 --&gt


(4)配置faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>

http://java.sun.com/xml/ns/javaee"
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">

myBean
com.devsphere.articles.jsfupload.MyBean
request



/MyForm.jsp

OK
/MyResult.jsp


(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

http://java.sun.com/jsf/core" prefix="f"%>
http://java.sun.com/jsf/html" prefix="h"%>
http://myfaces.apache.org/tomahawk" prefix="x"%>


value="#{myBean.myFile}"
storage="file"
required="true"/>


value="#{myBean.myParam}"
required="true">








action="#{myBean.processMyFile}"/>

MyResult.jsp

http://java.sun.com/jsf/core" prefix="f"%>
http://java.sun.com/jsf/html" prefix="h"%>





[@more@]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/11754099/viewspace-1023746/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/11754099/viewspace-1023746/

 类似资料: