您的位置:首页 > 编程语言 > Java开发

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

2016-03-19 20:37 423 查看
(本文章由作者自己所写,所有版权归作者所有,如要转载请将本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/javaeehttp://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

现在上传看一下,能不能实现了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: