webwork文件上传,用百度,狗狗,一搜就可以搜到很多相关的贴子,这其中不少贴子可称经典,可以载入史册.
在公司闲得没事做,就总结一下webwork的上传全过程.
首先我们得写一个webwork的配置文件(首先你得保证你这个工程加入了webwork)
代码如下:
第二,就得开发Action,简单的说就是开发java类.
代码如下:
package com.marty;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.servlet.http.HttpServletRequest;
import service.CasePigeonholeService;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
public class CasePigeonholeManager implements ServletRequestAware{
private CasePigeonholeService casePigeonholeService;
private domain.CasePigeonhole casePigeonhole;
private File[] cattas;//文件
private String[] cattasFileName;//文件名
private String[] cattasContentType; //内容类型
HttpServletRequest request=null;
public String execute()throws Exception{
return SUCCESS;
}
public String casePigeonholeAdd()throws Exception{
casePigeonholeService.saveCasePigeonhole(casePigeonhole,cattas,cattasFileName,cattasContentType
,request);
return SUCCESS;
}
@Override
public int getModuleRightId() {
// TODO Auto-generated method stub
return 0;
}
public CasePigeonholeService getCasePigeonholeService() {
return casePigeonholeService;
}
public void setCasePigeonholeService(CasePigeonholeService casePigeonholeService) {
this.casePigeonholeService = casePigeonholeService;
}
public domain.CasePigeonhole getCasePigeonhole() {
return casePigeonhole;
}
public void setCasePigeonhole(domain.CasePigeonhole casePigeonhole) {
this.casePigeonhole = casePigeonhole;
}
public File[] getCattas() {
return cattas;
}
public void setCattas(File[] cattas) {
this.cattas = cattas;
}
public String[] getCattasContentType() {
return cattasContentType;
}
public void setCattasContentType(String[] cattasContentType) {
this.cattasContentType = cattasContentType;
}
public String[] getCattasFileName() {
return cattasFileName;
}
public void setCattasFileName(String[] cattasFileName) {
this.cattasFileName = cattasFileName;
}
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
}
第三.开发SERVICE.
package service.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.http.HttpServletRequest;
import service.CasePigeonholeService;
import com.opensymphony.webwork.interceptor.ServletRequestAware;
import common.GetProperty;
import domain.CaseDocument;
import domain.CaseInfo;
import domain.CasePigeonhole;
public class CasePigeonholeServiceImpl implements
CasePigeonholeService{
public void saveCasePigeonhole(CasePigeonhole casePigeonhole,
File[] cattas, String[] cattasFileName, String[] cattasContentType,HttpServletRequest request)throws Exception{
for(int i=0;i
}
}
}
public void insertFileAtta(String caseID,
File atta, String attaFileName, String attaContentType,HttpServletRequest request) throws Exception {
//保存附件
CaseDocument caseDoc=new CaseDocument();
String propName ="caseUpload";
String value = "";
//Properties对象读取*.properties中的文件
Properties prop = new Properties();
try{
// String path = System.getProperty("user.dir"); //获得工程的路径名
String path=request.getSession().getServletContext().getRealPath("//WEB-INF//");
logger.info("path:"+path);
prop.load(new FileInputStream(path + File.separator +"uploadPath.properties"));
//从工程的路径中装载propeties文件
value = prop.getProperty(propName); //获取参数值
}catch(FileNotFoundException fnfe){
logger.info("file /"uploadPath.properties/" not found");
fnfe.printStackTrace();
}catch(IOException ioe){
logger.info("io exception");
ioe.printStackTrace();
}
//文件上传处理
String fileurl =value;//获取上传路径
logger.info("fileurl:"+fileurl);
FileTools.createDir(fileurl);
String tmp = attaFileName.substring(attaFileName.lastIndexOf(".") + 1);
String tmpfilename = String.valueOf(System.currentTimeMillis())
+ String.valueOf(Math.round(Math.random() * 1000D)) + "." + tmp;
caseDoc.setCase_file_name(tmpfilename);
caseDoc.setUrl_content(fileurl);
caseDoc.setCase_info_id(caseID);
FileOutputStream fos = new FileOutputStream(fileurl + tmpfilename);
FileInputStream is = new FileInputStream(atta);
FileTools.copyStream(is, fos);
is.close();
fos.close();
insert(caseDoc);
}
}
看我们的页面
看properties配置文件.
###when cvs a project please change
###the directory or create a directory
caseUpload=D:/zhudaoqin/workspace/xzzf-0822B/Webroot/upload/
好了.