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

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

2016-04-06 11:27 495 查看
无论是.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来接受和处理这些数据,也很easy,仅仅是我们要遵守对应的规则:

<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都已经具备,我们还要通过配置文件将他们二者联系起来。也就是通过以下的这些代码:

</pre><pre name="code" class="java"><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中;而当我们将信息输入好之后,点击“上传”button。数据又会被传送到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框架实现文件上传的功能解说就先到这里。希望能够帮到大家!假设大家有什么问题或者更好的建议。欢迎大家指正或者联系我!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: