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

Struts2实现文件上传(十四)

2017-12-29 14:12 609 查看
(一)实现文件上传

1.说明

在开发基于Web的应用的时候,经常会碰到需要实现文件上传、下载的功能,比如编辑某个商品,需要给它上传一个图片等等。不使用Struts2的话,可以有很多种方式来实现文件上传,比如使用Apache的Common-FileUpload等。但是这些传统的方式,实现起来非常麻烦,需要写很多代码来进行控制。 现在使用Struts2来实现文件上传的功能,会更加简单和方便,事实上,Struts2的文件上传功能,默认就是基于Common-FileUpload来实现的,只不过比直接使用Common-FileUpload来得更简单。

2.实现上传文件的必备条件

1.导入两个核心jar包:commons-fileupload-1.3.1.jar、commons-io-2.2.jar

2.提交表单的方式必须设置为:method=”post”,enctype=”multipart/form-data”

3.实现文件上传的步骤

1.创建文件上传表单

2.在项目中WebContent目录下创建一个文件存储目录。

3.创建文件上传Action类,编写4个属性封装对于的数据。

4.在struts.xml文件中配置Action类,指定上传文件的大小,指定文件写入磁盘的路径。

5.通过获取文件上传后保存到磁盘的路径,使用图片标签把它加载到JSP页面渲染。

(二)文件上传实例演示

1.创建文件上传表单

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<label for="myFile">选择上传文件</label>
<input type="file" name="myFile" />
<input type="submit" value="上传"/>
</form>
</body>
</html>


2.创建文件上传Action类,编写4个属性封装对于的数据。

package com.Upload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
*
* @author Kaina 2017-12-29
*
*/
public class UploadFile extends ActionSupport {

private static final long serialVersionUID = 1L;
private File myFile;// 1.使用文件类型封装文件域的内容
private String myFileContentType;// 2.封装上传文件的类型
private String myFileFileName;// 3.封装上传文件名
private String savePath;// 4.封装上传文件保存的路径
public String execute() throws Exception {
// 5.获取文件的本地真实路径(也就是文件上传保存的位置,可以把该路径+文件名插入到数据库)
// 上传文件保存路径:E:\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\FileUploadDemo\Files
String directory = ServletActionContext.getServletContext().getRealPath("/Files");
System.out.println(directory );
File file = new File(directory);

// 6.将文件名添加到保存文件的路径下,并可以把f插入到数据库
File f = new File(file, myFileFileName);
myFile.renameTo(f);
System.out.println(f);
// 7.将保存在项目文件Files中的文件写到指定盘中(下载)
@SuppressWarnings("resource")
FileInputStream fis = new FileInputStream(f);
@SuppressWarnings("resource")
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"+getMyFileFileName());
byte[] content = new byte[1024];
int len = 0;
while ((len = fis.read(content)) != -1) {
fos.write(content, 0, len);
}
fis.close();
fos.close();
return SUCCESS;
}

public File getMyFile() {
return myFile;
}

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

public String getMyFileContentType() {
return myFileContentType;
}

public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}

public String getMyFileFileName() {
return myFileFileName;
}

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

public String getSavePath() {
// 获取文件绝对路径
// return
// ServletActionContext.getServletContext().getRealPath(savePath);
return savePath;
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}
}


注:

1.获取文件的本地真实路径的三种方式:

01.String targetDirectory = context.getRealPath("/upload");

02.String tupi = getMyFileFileName();

03.String path = ServletActionContext.getServletContext().getRealPath("/");


3.在struts.xml文件中配置Action类,指定上传文件的大小,指定文件写入磁盘的路径。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<!--1.设置允许上传文件的大小-->
<constant name="struts.multipart.maxSize" value="3000000" />

<package name="helloworld" extends="struts-default">
<action name="upload" class="com.Upload.UploadFile">
<!--2.设置文件读入后,并写到磁盘的位置-->
<param name="savePath">D:/</param>
<!--<param name="maximumsize">3000000</param>-->
<!--3.上传文件成功后显示文件-->
<result name="success">/picture.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>


注意:

1.如果需要过滤文件类型,就需要在struts.xml文件上传Action标签中引入fileUpload拦截器。
比如允许通过的图片:
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/png,image/jpeg,image/gif</param>
</interceptor-ref>

2.文件比如:.txt .jsp .py等等处理方式一样

3.设置文件上传大小可以在constant标签中配置,也可以在param标签中配置。文件存储路径的设置也一样。


4.通过获取文件上传后保存到磁盘的路径,使用图片标签把它加载到JSP页面渲染。

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=UTF-8">
<title>图片上传结果</title>
</head>
<body>
<table border="1">
<tr>
<th>编号</th>
<th>图片</th>
</tr>
<tr>
<th>01</th>
<th><img src="<s:property value=" 'Files/'+myFileFileName"/>"/></th>
</tr>
</table>
</body>
</html>


5.结果演示



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