今天写了一个web网页上传文件的功能、这也是一个很常见的功能、我使用的是struts2自带的一个插件需要导入以下几个包:
commons-logging-1.1.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.6.jar
xwork-2.0.1.jar
commons-io-1.3.1.jar
commons-fileupload-1.2.jar
上传文件页面要包含一个
<s:file name="upload" label="上传包"></s:file>
接着写我们在action
public class PlatformAction extends BaseAction {
/**
*
*/
private static final long serialVersionUID = -2775753048076575261L;
//上传文件(这里要用File对象来封装)
private File upload;
//下面两个属性的get和set方法要以upload的get和set打头来命名,最简单的就是upload+xxxx 就可以去自动生成了
private String uploadContentType; // 文件的内容类型
private String uploadFileName; // 上传文件名
/**
*
* 方法名:saveServicePack 描述: 作者:白鹏飞 日期:2012-7-13 下午02:42:31
*
* @param @return
* @param @throws BzException
* @return String
*/
public String saveServicePack() throws BzException {
//检测记录是否存在
getEasyServicePackService().checkPackInfo(getPackName(), getVersion());
//检测服务器是否有同名软件包
File fDir = new File(Constant.SERVICE_PACK_ADDRESS);
// File fDir = new File("c:/");
//检测路径
if(!fDir.exists()){
fDir.mkdirs();
}
String[] fNames = fDir.list();
for(int i=0;i<fNames.length;i++){
if(fNames[i].equals(this.getUploadFileName())){
throw new BzException("服务包重名");
}
}
//上传软件包
try {
//基于myFile创建一个文件输入流
InputStream is = new FileInputStream(getUpload());
// 创建一个输出流
// OutputStream os = new FileOutputStream(new File("c:/", this.getUploadFileName()));
OutputStream os = new FileOutputStream(new File(Constant.SERVICE_PACK_ADDRESS, this.getUploadFileName()));
//设置缓存
byte[] buffer = new byte[1024];
int length = 0;
//读取File文件输出到toFile文件中
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
//关闭输入流
is.close();
//刷新
os.flush();
//关闭输出流
os.close();
} catch (Exception e) {
// TODO Auto-generated catch block
throw new BzException("文件上传失败");
}
// 获取Session对象将UserVO放入Session
ActionContext actionContext = ActionContext.getContext();
session = actionContext.getSession();
EasyUser user = (EasyUser) session.get(Constant.SESSION_OPERTAOR);
getEasyServicePackService().saveServicePack(getPackName(),
getVersion(), getEnabled(), user.getUserid(), getInstallDir(),
getPlatId(), getIspublic(), getServerName());
setActionMsg("上传成功");
return SUCCESS;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
}
接着配置我们的web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样就可以了,但是我们写完进行测试时会发现稍微大一点的文件struts2就会报input错误,这个原因是因为struts2默认的文件大小是2M超过这个数值程序就会认为处理不了Action也不会去执行我们
我们可以在struts.properties或者struts.xml里面配置
<constant name="struts.multipart.maxSize" value="104857600" />
在struts.xml里面加入这一句话就可以上传100M的文件
当然上传文件的功能还不只如此还可以设定限制上传的文件和文件类型不匹配和文件过大异常的处理
google一搜一大堆、这里就不再赘述了。