坌鸟先飞之Java(一)-使用struts2框架实现文件上传和下载

笨鸟先飞之Java(一)--使用struts2框架实现文件上传和下载

    不管是.net还是Java,我们最常接触到的就是文件的上传和下载功能,在Java里要实现这两个常用功能会有很多种解决方式,但是struts2的框架却能给我们一个比较简单的方式,下面就一起来看吧:

文件上传:

    首先来看实现上传功能的表单,Index.jsp:

<span style="font-family:FangSong_GB2312;font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<form>
		标题:<input type="text" name="title"><br>
		文件:<input type="file" name="myFile"><br>
		<input type="submit" value="上传">
	</form>
</body>
</html></span>

     当我们上传成功之后需要一个jsp来显示上传文件的相关信息,所以我们新建一个jsp文件,代码如下:

<span style="font-family:FangSong_GB2312;font-size:18px;"><%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	title:${title }<br>
	fileName:${myFileFileName }<br>
</body>
</html></span>

    在struts2这个框架中,有了jsp,我们还要使用Action来接受和处理这些数据,也非常简单,只是我们要遵守相应的规则:

<span style="font-size:18px;"><span style="font-family:FangSong_GB2312;">package com.bjpowernode.struts2;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import com.opensymphony.xwork2.Action;

/**
 * 文件上传的Action
 * @author chao
 *
 */
public class UploadTestAction {

	private String title;
	
	//可以得到上传文件的名称
	//规则:输入域的名称+固定字符串FileName
	private String myFileFileName;
	
	//取得文件数据
	//规则:File输入域的名称
	private File myFile;
	
	//取得内容类型
	//规则:输入域的名称+固定字符串ContextType
	private String myFileContextType;
	
	public String getMyFileFileName() {
		return myFileFileName;
	}

	public void setMyFileFileName(String myFileFileName) {
		this.myFileFileName = myFileFileName;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public File getMyFile() {
		return myFile;
	}

	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	public String getMyFileContextType() {
		return myFileContextType;
	}

	public void setMyFileContextType(String myFileContextType) {
		this.myFileContextType = myFileContextType;
	}

	public String execute() throws Exception{
		//初始化输入流和输出流
		InputStream is =null;
		OutputStream os = null;
		try{
			//读入文件
			is = new BufferedInputStream(
					new FileInputStream(myFile));
			//读出文件到既定位置
			os = new BufferedOutputStream(
				    new FileOutputStream("c:\\" + myFileFileName));
			//实例化一个byte数组
			byte[] buffer = new byte[1024];
			int len = 0;
			while((len=is.read(buffer)) > 0){
				os.write(buffer,0,len);
			}
		}finally{
			if(is != null){is.close();}
			if(os != null){os.close();}
		}
		return Action.SUCCESS;
</pre><pre name="code" class="java">

        当jsp和Action都已经具备,我们还要通过配置文件将他们二者联系起来,也就是通过下面的这些代码:


<span style="font-family:FangSong_GB2312;font-size:18px;">
</span>
</pre><pre>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <!--当struts.xml配置文件发生修改,会立刻加载;在生产环境下最好不要配置 -->
    <constant name="struts.configuration.xml.reload" value="true"/>
    <!--设置该配置可以展现更友好的错误提示界面  -->
    <constant name="struts2.devMode" value="true"/>
    <!--需要继承struts-default包,这样就拥有了最基本的功能  -->
	<package name="upload-package" extends="struts-default">
		<action name="upload" class="com.bjpowernode.struts2.UploadTestAction">
			<result>/success.jsp</result>
		</action>
	</package>
</struts>
     

    这样一来,我们就可以将Action接手到的数据反馈到Index.jsp中;而当我们将信息输入好之后,点击“上传”按钮,数据又会被传送到Action中,如果上传成功,则会转向到配置文件中的seccess.jsp中。


    所以,使用struts2的框架来实现文件的上传功能并不难,主要是利用IO流来实现。看到这里,您有没有想过如果要实现多文件的上传,又该如何?下面一起来看:

    在struts2中,多文件上传和单文件上传原理相同,只是要使用多个<s:file/>标签绑定Action的数组。绑定到数组的Action代码就应该是:

     private File[] uploads;
     private String[] uploadFileNames;
     private String[] uploadContentTypes;

     public File[] getUpload() { return this .uploads; } 
     public void setUpload(File[] upload) { this .uploads = upload; } 
 
     public String[] getUploadFileName() { return this .uploadFileNames; } 
     public void setUploadFileName(String[] uploadFileName) { this .uploadFileNames = uploadFileName; } 
     //上传文件的类型
     public String[] getUploadContentType() { return this .uploadContentTypes; } 
     public void setUploadContentType(String[] uploadContentType) { this .uploadContentTypes = uploadContentType; }
注意:

1、struts2默认采用了apache commons-fileupload

2、struts2支持三种类型的上传组件

3、需要引入commons-fileupload相关的jar包
   * commons-io-1.3.2.jar
   * commnos-fileupload-1.2.1.jar
4、表单中需要采用post提交方式,编码类型需要使用multipart/form-data
5、struts2的Action
   取得文件名称---->>>规则:输入域的名称+固定字符串FileName
   取得文件数据---->>>规则:File输入域的名称
   取得内容类型---->>>规则:输入域的名称+固定字符串ContextType

6.得到输入流,采用输出流写文件


    使用struts2框架实现文件上传的功能讲解就先到这里,希望可以帮到大家!如果大家有什么问题或者更好的建议,欢迎大家指正或者联系我!

          坌鸟先飞之Java(一)-使用struts2框架实现文件上传和下载



版权声明:本文为博主原创文章,未经博主允许不得转载。