struts2上传文件(三) struts2上传文件

(本文章由作者自己所写,所有版权归作者所有,如要转载请将本blog地址附上)

此文章写好了久了,今天找出来了,再发表一下。

 前面做了这么多的铺垫,这次真的要好好讲一下,struts怎么上传了。struts2是怎么回事这里就不说了,自己去看看struts2 我们只讲怎么上传。
工具版本:jdk6 , myeclipse 7 , tomcat6 
首先我们来建一个新的WEB项目。

package com.garry.struts.upload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import javax.servlet.http.HttpServletRequest;

import com.opensymphony.xwork2.ActionSupport;

//继承ActionSupport是因为可以加验证
public class UploadFile extends ActionSupport {

/**

*/
private static final long serialVersionUID = 1L;
//这里面所有的upload开头的意思是说,在表单里上传的<input type='file' name='upload'>   名字一定要对,不然会有异常

private HttpServletRequest request ;
//封装文件标题请求参数的属性
private String title;
//封装上传文件域属性
private File upload;
//封装上传文件类型的属性
private String uploadContentType;
//封装上传文件名的属性
private String uploadFileName;
// 接受依赖注入的属性
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
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;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}



public String execute() throws Exception{

//以服务器保存地址和原文件名建立上传文件输出流
FileOutputStream fos = new FileOutputStream(getSavePath()+"());
//以上传文件建立一个文件上传流
FileInputStream fis = new FileInputStream(getUpload());

//以上传文件的内容写入服务器
System.out.println(upload.getPath());
byte[] buffer = new byte[1024];

int len = 0 ; 
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
return SUCCESS;
}
public void setServletRequest(HttpServletRequest arg0) {
// TODO Auto-generated method stub
this.request = arg0;
}

}

到这里,我们就把类写完了,然后我们再需要配置一些配置文件,我们先来配置一下web.xml 如果会出现一些异常,就把里面的注释去掉

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="
http://java.sun.com/xml/ns/javaee
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 
<filter>
<filter-name>clean-struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>

<filter-mapping>
<filter-name>clean-struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
-->
</web-app>

web.xml配置好以后,我们来配置一下最关键的struts.xml  

<?xml version="1.0" encoding="UTF-8"?>
<!-- 指定struts2 配置文件的DTD信息 -->

<!DOCTYPE struts PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"
http://struts.apache.org/dtds/struts-2.0.dtd">

<!-- struts 2 的action 必需放在指定的包空间下定义 -->

<struts>
<!--第一条是配置上传文件,第二条是配置编码-->
<constant name="struts.mutlipart.parser" value="jakarta"></constant>
<constant name="struts.i18n.encoding" value="utf-8"/>

<package name="up" extends="struts-default">
<action name="upload" class="com.garry.struts.upload.UploadFile">
<param name="savePath">D:\\tomcat6\\webapps\\uploadstruts2\\upload</param>
<result>succ.jsp</result>
</action>
</package>
</struts>

所有的做完了,最后怎么办呢?我们还有两个文件没有放进WEB-INF/lib文件夹里。就是那两个文件
commons-fileupload-1.2.1.jar
commons-io-1.4.jar

现在上传看一下,能不能实现了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值