[简单介绍]
webwork 文件上传采用拦截器的方式,叫fileUpload
可供选择的上传组件有pell(2.1.7默认)jakata(2.2及以后默认),cos
[例子]
[注意事项]
1、cos和pell文件上传解析器不在被积极维护,会被很快移除.我们强烈建议你使用Jakarta的文件上传解析器,也就是缺省的解析器
2、处理multipartRequest pell支持中文属性和中文文件名,如果遇到中文乱码问题,建议使用pell
step1:
页面定义如下
......................... 省略
step2
action 如下
public class UploadAction extends ActionSupport {
.....................................
private File doc;//doc与页面取一样的名字
public void setDoc(File doc) {
this.doc = doc;
}
//当然还可以使用以下的咚咚
/*
* public void setDocContentType(String docContentType) {
* this.docContentType = docContentType; }
*
*//**
*
* @author weip
* @time 0:23:24 2006-5-11
* @param docFileName
*/
/*
* public void setDocFileName(String docFileName) { this.docFileName =
* docFileName; }
*/
/**
*
*/
/*
* private String docContentType;
*
*//**
*
*/
/*
* private String docFileName;
*/
.............................................................
}
当程序进入到action当中时,你可以开始处理上面代码中的doc,此时文件已上传到服务器中的一个临时目录(同时被注入到action中就是你看到的doc),此临时目录由webwork.properties指定,你必须将此文件拷贝到你需要的目录,否则action执行完毕,此文件会被删除,准确说是拦截器的后处理删除了临时文件,这一段结论的前提是你使用拦截器处理上传
step3
处理配置文件,配置拦截器
step4
对于webwork.properties的一些处理
webwork in action上推荐使用webwork.multipart.parser=jakarta而不是默认的 pell,如果使用前者,不要忘了在你的lib中加入commons-fileupload.jar
需要注意的问题:
你在使用fileUpload传拦截器的时候应该避免使用execAndWait拦截器,否则很有可能会出现空指针,因为execAndWait拦截器会在另一个线程处理action,而原来的线程会删掉临时文件,所以当你在action中处理文件的时候很有可能文件已被删掉,虽然这并非一定会出现,但毕竟不安全
转自:http://blog.csdn.net/pwlazy/archive/2006/05/11/724735.aspx
方法二、(有多文档上传那块)
用webwork框架做upload的java读取代码
我的语言表达能力不是很好,说也许说不明白,可能给看的说得糊涂了,同样
这个对做上传得新手很有用,不啰嗦了,先写代码吧:一共4个地方要弄,
前台,后台,xwork和webwork.properties
前台:
upload.jsp:
=================================================================
<%--
Created by IntelliJ IDEA.
User: lvjiachun
Date: 2006-5-3
Time: 9:32:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="ww" uri="/webwork" %>
=======================================================================
upload-success.jsp
=======================================================================
<%--
Created by IntelliJ IDEA.
User: lvjiachun
Date: 2006-5-3
Time: 9:38:04
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="ww" uri="/webwork" %>
你已经上传成功,
请返回
========================================================================
后台:一共需要三个4个java文件
1.
package upload.until;
/**
* Created by IntelliJ IDEA.
* User: lvjiachun
* Date: 2006-5-3
* Time: 7:51:11
* To change this template use File | Settings | File Templates.
*/
public class FileMes {
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
private String filePath;
}
2.
package upload.until;
/**
* Created by IntelliJ IDEA.
* User: lvjiachun
* Date: 2006-5-3
* Time: 7:51:23
* To change this template use File | Settings | File Templates.
*/
public class FileSelect {
private String fileName;
private String fileURL;
private String type;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileURL() {
return fileURL;
}
public void setFileURL(String fileURL) {
this.fileURL = fileURL;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
3.
package upload.until;
import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequestWrapper;
import com.opensymphony.webwork.dispatcher.multipart.MultiPartRequest;
import com.opensymphony.webwork.ServletActionContext;
import java.util.List;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Calendar;
import java.io.*;
/**
* Created by IntelliJ IDEA.
* User: lvjiachun
* Date: 2006-5-3
* Time: 7:51:00
* To change this template use File | Settings | File Templates.
*/
public class UploadUntil {
public static List
List
String path = smallPath;
System.out.println("PATH 2:" + path);
MultiPartRequestWrapper multiWrapper = (MultiPartRequestWrapper) ServletActionContext.getRequest();
if (multiWrapper == null) return null;
Enumeration e = multiWrapper.getFileNames();
int i = 0;
//===============================================================//
//这个是单文件上传的代码;!!如果想做单文件上传,把注释掉的代码去掉就可以了
// while (e.hasMoreElements()) {
// String inputValue = (String) e.nextElement();
// String contentType = multiWrapper.getContentType(inputValue);
// String flie1 = multiWrapper.getFilesystemName(inputValue);
// String fileName = flie1;
// System.out.println("FileName underfinded:" + fileName);
// String savePath = path + fileName;
// File file = multiWrapper.getFile(inputValue);
// if (file != null) {
// InputStream inStream = new FileInputStream(file.getPath());
// FileMes fm = new FileMes();
// fm.setFileName(flie1);
// System.out.println("FileName:" + fm.getFileName());
// System.out.println("SmallPath in UploadUntil.java" + smallPath);
// fm.setFilePath(smallPath + fileName);
// System.out.println("FilePath:" + fm.getFilePath());
// fileMesList.add(fm);
// System.out.println("SavePath:" + savePath);
// FileOutputStream fs = new FileOutputStream(savePath);
// byte[]buffer = new byte[1444];
// int length;
// int bytesum = 0;
// int byteread = 0;
// while ((byteread = inStream.read(buffer)) != -1) {
// bytesum += byteread;
// fs.write(buffer, 0, byteread);
// }
//============================================================//
// 下面这个是多文件上传-------------------
String inputValue = (String) e.nextElement();
String contentType = multiWrapper.getContentType(inputValue);
String flie1 = multiWrapper.getFilesystemName(inputValue);
String fileName = Calendar.getInstance().getTime().getTime() + "-" + i + flie1;
String savePath = path + fileName;
File file = multiWrapper.getFile(inputValue);
if (file != null) {
InputStream inStream = new FileInputStream(file.getPath()); //读入原文件
FileMes fm = new FileMes();
fm.setFileName(flie1);
fm.setFilePath(smallPath + fileName);
fileMesList.add(fm);
FileOutputStream fs = new FileOutputStream(savePath);
byte[] buffer = new byte[1444];
int length;
int bytesum = 0;
int byteread = 0;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
fs.write(buffer, 0, byteread);
}
//==============================================================//
inStream.close(); //关闭输入流
i++;
if (file.exists()) {
file.delete();
} //如果上传的文件存在则将其删除;
}
}
return fileMesList;
}
}
4.也就是最后一个,action
package upload.fileupload;
import com.opensymphony.xwork.ActionSupport;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;
import upload.until.FileSelect;
/**
* Created by IntelliJ IDEA.
* User:lvjiachun
* Date: 2006-5-3
* Time: 7:52:34
* To change this template use File | Settings | File Templates.
*/
public class UploadfilesAction extends ActionSupport implements Action, ModelDriven {
FileSelect fileMes = new FileSelect();
public String upload() throws Exception {
String smallPath = fileMes.getFileURL();
System.out.println(smallPath+"xiaosao");
java.io.File myFilePath = new java.io.File(smallPath);
if (smallPath != null) {
if (!myFilePath.exists()) {
myFilePath.mkdir();
System.out.println("*******MaKe DIR!*********");
}
}
upload.until.UploadUntil.Upload(smallPath);
return SUCCESS;
}
public Object getModel() {
return fileMes; //To change body of implemented methods use File | Settings | File Templates.
}
}
**********************************************************************
xwork中
http://www.opensymphony.com/xwork/xwork-1.1.1.dtd">
/upload/upload-success.jsp
webwork.properties
在webwork.properties中要加上这段代码
webwork.multipart.parser=com.opensymphony.webwork.dispatcher.multipart.PellMultiPartRequest
webwork.multipart.saveDir=d:// #这个是上传文件的默认保存路径#