/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.Part;
/**
*
* @author Administrator
*/
@WebFilter(filterName = "FileUploadFilter", urlPatterns = {"/upload.jsf"},initParams = {
@WebInitParam(name="path", value="c:/uploadFile/")
})
public class FileUploadFilter implements Filter {
private static final boolean debug = true;
private FilterConfig filterConfig = null;
public FileUploadFilter() {
}
/**
*
* @param request The servlet request we are processing
* @param response The servlet response we are creating
* @param chain The filter chain we are processing
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
if (debug) {
log("FileUploadFilter:doFilter()");
}
Throwable problem = null;
try {
if (!(request instanceof HttpServletRequest)) {
chain.doFilter(request, response);
return;
}
HttpServletRequest httpRequest = (HttpServletRequest) request;
if (!FileUploadUtil.isMultipartContent(httpRequest)) {
chain.doFilter(request, response);
return;
}
Collection<Part> parts = null;
try {
parts = httpRequest.getParts();
Iterator<Part> items = parts.iterator();
final Map<String,String[]> paramMap = new HashMap<String, String[]>();
while (items.hasNext()) {
Part part = items.next();
if(!"uploadFile".equals(part.getName())){
String name = part.getName();
String[] values = (String[]) paramMap.get(name);
if (values == null) {
// Not in parameter map yet, so add as new value.
paramMap.put(name, new String[] {new BufferedReader(new InputStreamReader(part.getInputStream())).readLine()});
} else {
// Multiple field values, so add new value to existing array.
int length = values.length;
String[] newValues = new String[length + 1];
System.arraycopy(values, 0, newValues, 0, length);
newValues[length] = new BufferedReader(new InputStreamReader(part.getInputStream())).readLine();
}
}else{
String fullFilename = null;
for (String str : part.getHeader("content-disposition").split(";")) {
if (str.trim().startsWith("filename")) {
fullFilename = str.substring(str.indexOf('=') + 1).trim().replace("\"", "");
break;
}
}
String path = getFilterConfig().getInitParameter("path");
String uploadedFileName = path+fullFilename.substring(fullFilename.lastIndexOf(File.separator)+1,fullFilename.length());
System.out.println("File:"+uploadedFileName);
//write file to disk
InputStream is = part.getInputStream();
OutputStream os = new FileOutputStream(uploadedFileName);
byte[] buffer = new byte[2048];
int read = 0;
int count = 0;
while((read=is.read(buffer))>0){
os.write(buffer, 0, read);
if(count==50){
os.flush();
count = 0;
}
count++;
}
os.close();
is.close();
File uploadedFile = new File(uploadedFileName);
httpRequest.setAttribute(part.getName(),uploadedFile);
part.delete();
}
}
chain.doFilter(new HttpServletRequestWrapper(httpRequest) {
//inner methods passed as parameters
@Override
public Map<String, String[]> getParameterMap() {
return paramMap;
}
@Override
public String[] getParameterValues(String name) {
Map<String, String[]> map = getParameterMap();
return (String[]) map.get(name);
}
@Override
public String getParameter(String name) {
String[] params = getParameterValues(name);
if (params == null) {
return null;
}
return params[0];
}
@Override
public Enumeration<String> getParameterNames() {
Map<String, String[]> map = getParameterMap();
return Collections.enumeration(map.keySet());
}
}/*end of inner methods*/, response);
} catch (Exception e) {//no file item
e.printStackTrace();
}
} catch (Throwable t) {
// If an exception is thrown somewhere down the filter chain,
// we still want to execute our after processing, and then
// rethrow the problem after that.
problem = t;
t.printStackTrace();
}
// If there was a problem, we want to rethrow it if it is
// a known type, otherwise log it.
if (problem != null) {
if (problem instanceof ServletException) {
throw (ServletException) problem;
}
if (problem instanceof IOException) {
throw (IOException) problem;
}
sendProcessingError(problem, response);
}
}
/**
* Return the filter configuration object for this filter.
*/
public FilterConfig getFilterConfig() {
return (this.filterConfig);
}
/**
* Set the filter configuration object for this filter.
*
* @param filterConfig The filter configuration object
*/
public void setFilterConfig(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}
/**
* Destroy method for this filter
*/
public void destroy() {
}
/**
* Init method for this filter
*/
public void init(FilterConfig filterConfig) {
this.filterConfig = filterConfig;
if (filterConfig != null) {
if (debug) {
log("FileUploadFilter:Initializing filter");
}
}
}
/**
* Return a String representation of this object.
*/
@Ov
没有合适的资源?快使用搜索试试~ 我知道了~
jsf2.0上传组件(servlet3.0)
共43个文件
xml:15个
java:7个
xhtml:4个
5星 · 超过95%的资源 需积分: 9 103 下载量 72 浏览量
2009-12-29
17:18:26
上传
评论
收藏 356KB RAR 举报
温馨提示
利用JSF2.0和servlet3.0做的上传的例子,并实现单个文件上传的组件,虽然有些小限制,但是对单个上传功能完全可以实现。花了一整天的时间来研究啊,不容易!顶起吧。有什么想法请发表评论
资源推荐
资源详情
资源评论
收起资源包目录
Upload.rar (43个子文件)
Upload
test
build.xml 3KB
catalog.xml 217B
dist
JSF2.war 261KB
src
conf
MANIFEST.MF 25B
java
com
FileUpload.java 2KB
User.java 795B
test
Test.java 428B
FileUploadFilter.java 10KB
FileUploadServlet.java 3KB
TestBean.java 1KB
FileUploadUtil.java 736B
nbproject
build-impl.xml 46KB
faces-config.NavData 476B
private
profiler
configurations.xml 8KB
private.xml 706B
retriever
www.w3.org
2001
xml.xsd 9KB
java.sun.com
xml
ns
javaee
javaee_5.xsd 62KB
javaee_web_services_client_1_2.xsd 19KB
web-facelettaglibrary_2_0.xsd 19KB
catalog.xml 675B
private.properties 3KB
profiler-build-impl.xml 2KB
ant-deploy.xml 4KB
project.xml 880B
genfiles.properties 652B
project.properties 2KB
web
newTemplate.xhtml 1KB
WEB-INF
web.xml 1011B
upload.tld 491B
faces-config.xml 830B
sun-web.xml 507B
resources
css
cssLayout.css 1000B
default.css 680B
js
jquery.js 84KB
upload.xhtml 1KB
index.xhtml 2KB
user.xhtml 931B
build-before-profiler.xml 3KB
build
web
WEB-INF
web.xml 1011B
upload.tld 491B
sun-web.xml 505B
lib
commons-fileupload-1.2.1.jar 56KB
commons-io-1.3.2.jar 86KB
共 43 条
- 1
资源评论
- k2136028452012-12-15原理都差不多
- ajan1111112012-06-22比较简洁,原理都差不多,都是封装request
yyq2008
- 粉丝: 3
- 资源: 13
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功