JSF的Sun 1.2實現增加Apache tomahawk實現檔案上傳

zhanghjgnu發表於2009-07-03

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=""
xsi:schemaLocation=" "
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

xmlns:xsi=""
xsi:schemaLocation=" "
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




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


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








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

MyResult.jsp






[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/11754099/viewspace-1023746/,如需轉載,請註明出處,否則將追究法律責任。

相關文章