java框架Struts学习--文件上传与下载

1、导jar包

2、配置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">
	

	<filter>
		<filter-name>Struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>Struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

3、全局配置struts.xml

<?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>

	<!-- 二、总配置文件:引入其他所有配置文件 -->
	
	<include file="constant.xml"></include>
	<include file="cn/itcast/a_config/struts.xml"></include>
	<include file="cn/itcast/b_config2/config.xml"></include>
	<include file="cn/itcast/c_data/data.xml"></include>
	<include file="cn/itcast/d_type/type.xml"></include>
	<include file="cn/itcast/e_fileupload/upload.xml"></include>
	
</struts>


4、actionxml配置

constant.xml

<?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>

	<!-- 一、全局配置 -->
	<!-- 0. 请求数据编码 -->
	 <constant name="struts.i18n.encoding" value="UTF-8"/>
	<!-- 1. 修改Struts默认的访问后缀 -->
	<constant name="struts.action.extension" value="action,do,"></constant>
	<!-- 2. 修改xml自动重新加载 -->
	<constant name="struts.configuration.xml.reload" value="true"/>
	<!-- 3. 开启动态方法调用 (默认不开启)-->
	<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
	<!-- 4. 修改上传文件的最大大小为30M -->
	<constant name="struts.multipart.maxSize" value="31457280"/>
	
	
</struts>


upload.xml

<?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>

	<package name="upload_" extends="struts-default">
		<!-- 注意: action 的名称不能用关键字"fileUpload" -->
		<action name="fileUploadAction" class="cn.itcast.e_fileupload.FileUpload">
		
			<!-- 限制运行上传的文件的类型 -->
			<interceptor-ref name="defaultStack">
				
				<!-- 限制运行的文件的扩展名 -->
				<param name="fileUpload.allowedExtensions">txt,jpg,jar</param>
				
				<!-- 限制运行的类型   【与上面同时使用,取交集】
				<param name="fileUpload.allowedTypes">text/plain</param>
				-->
				
			</interceptor-ref>
			
			<result name="success">/e/success.jsp</result>
			
			<!-- 配置错误视图 -->
			<result name="input">/e/error.jsp</result>
		</action>
		
		<action name="down_*" class="cn.itcast.e_fileupload.DownAction" method="{1}">
			<!-- 列表展示 -->
			<result name="list">/e/list.jsp</result>
			<!-- 下载操作 -->
			<result name="download" type="stream">
			
				<!-- 运行下载的文件的类型:指定为所有的二进制文件类型 -->
			   <param name="contentType">application/octet-stream</param>
			   
			   <!-- 对应的是Action中属性: 返回流的属性【其实就是getAttrInputStream()】 -->
			   <param name="inputName">attrInputStream</param>
			   
			   <!-- 下载头,包括:浏览器显示的文件名 -->
			   <param name="contentDisposition">attachment;filename=${downFileName}</param>
			 
			 	<!-- 缓冲区大小设置 -->
			   <param name="bufferSize">1024</param>
			</result>
		</action>
	</package>	
</struts>


5、页面代码

upload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
  	<form action="${pageContext.request.contextPath }/fileUploadAction" method="post" enctype="multipart/form-data">
  		用户名:<input type="text" name="userName"><br/>
  		文件:<input type="file" name="file1"><br/>
  		
  		<input type="submit" value="上传">
  	</form>
  </body>
</html>
下载页面list.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>下载列表</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
  </head>
  
  <body>
  	<table border="1" align="center">
  		<tr>
  			<td>编号</td>
  			<td>文件名</td>
  			<td>操作</td>
  		</tr>
  		<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  		<c:forEach var="fileName" items="${fileNames}" varStatus="vs">
  			<tr>
  				<td>${vs.count }</td>
  				<td>${fileName }</td>
  				<td>
  					<!-- 构建一个url -->
  					<c:url var="url" value="down_down">
  						<c:param name="fileName" value="${fileName}"></c:param>
  					</c:url>
  					
  					<a href="${url }">下载</a>
  				</td>
  			</tr>
  		</c:forEach>
  	</table>
  </body>
</html>


6.action代码;

FileUpload.java

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload extends ActionSupport {

	// 对应表单:<input type="file" name="file1">
	private File file1; 
	// 文件名
	private String file1FileName;
	// 文件的类型(MIME)
	private String file1ContentType;
	public void setFile1(File file1) {
		this.file1 = file1;
	}
	public void setFile1FileName(String file1FileName) {
		this.file1FileName = file1FileName;
	}
	public void setFile1ContentType(String file1ContentType) {
		this.file1ContentType = file1ContentType;
	}
	
	
	@Override
	public String execute() throws Exception {
		/******拿到上传的文件,进行处理******/
		// 把文件上传到upload目录
		
		// 获取上传的目录路径
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		// 创建目标文件对象
		File destFile = new File(path,file1FileName);
		// 把上传的文件,拷贝到目标文件中
		FileUtils.copyFile(file1, destFile);
		
		return SUCCESS;
	}
}
DownAction.java

import java.io.File;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 文件下载
 * 1. 显示所有要下载文件的列表
 * 2. 文件下载
 * @author Jie.Yuan
 *
 */
public class DownAction extends ActionSupport {
	
	
	/*************1. 显示所有要下载文件的列表*********************/
	public String list() throws Exception {
		
		//得到upload目录路径
		String path = ServletActionContext.getServletContext().getRealPath("/upload");
		// 目录对象
		File file  = new File(path);
		// 得到所有要下载的文件的文件名
		String[] fileNames =  file.list();
		// 保存
		ActionContext ac = ActionContext.getContext();
		// 得到代表request的map (第二种方式)
		Map<String,Object> request= (Map<String, Object>) ac.get("request");
		request.put("fileNames", fileNames);
		return "list";
	}
	
	
	/*************2. 文件下载*********************/
	
	// 1. 获取要下载的文件的文件名
	private String fileName;
	public void setFileName(String fileName) {
		// 处理传入的参数中问题(get提交)
		try {
			fileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		// 把处理好的文件名,赋值
		this.fileName = fileName;
	}
	
	//2. 下载提交的业务方法 (在struts.xml中配置返回stream)
	public String down() throws Exception {
		return "download";
	}
	
	// 3. 返回文件流的方法
	public InputStream getAttrInputStream(){
		return ServletActionContext.getServletContext().getResourceAsStream("/upload/" + fileName);
	}
	
	// 4. 下载显示的文件名(浏览器显示的文件名)
	public String getDownFileName() {
		// 需要进行中文编码
		try {
			fileName = URLEncoder.encode(fileName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException(e);
		}
		return fileName;
	}

	
}



Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值