Struts2实现文件下载

index.jsp

<a href="resource/getCoursewareAction">下载课件</a>


showCoursewareList.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>
	<h1>课件列表</h1>
	<%
		int i = 0;
	%>
	<table border="1">
		<tr>
			<th>序号</th>
			<th>文件名</th>
			<th>下载次数</th>
			<th>上传时间</th>
			<!-- 删除操作,只有老师可见 -->
			<th>删除课件</th>
		</tr>
		<s:iterator value="#request.CoursewareList" id="courseware">
			<tr>
				<td><%=++i%></td>
				<td><a
					href="downloadAction?fileName=<s:property value="#courseware.name"/>"><s:property
							value="#courseware.name" /></a></td>
				<td><s:property value="#courseware.downloadTimes" /></td>
				<td><s:property value="#courseware.uploadDate" /></td>
				<!-- 删除课件的操作,只有老师可见 ,暂时先全部显示出来,之后用session判断用户角色是什么-->
				<td><a href="deleteCoursewareAction?coursewareId=<s:property value="#courseware.Rid"/>">删除</a></td>
			</tr>
		</s:iterator>
	</table>
	<s:debug></s:debug>
</body>
</html>


ResourceAction.java(接上一篇文件上传,这里给出ResourceAction.java的所有代码)

package edu.fzu.softwareengineer.courseSite.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

import edu.fzu.softwareengineer.courseSite.domain.Resource;
import edu.fzu.softwareengineer.courseSite.service.StudentService;
import edu.fzu.softwareengineer.courseSite.service.TeacherService;

public class ResourceAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<File> upload;
	private List<String> uploadContentType;
	private List<String> uploadFileName;
	private long maximumSize;
	private String allowedTypes;
	private Integer coursewareId;

	private final String COURSEWARE = "courseware";
	private final String HOMEWORK = "homework";

	private Resource resource;
	private TeacherService teacherService;
	private StudentService studentService;

	// 下载的文件名
	private String fileName;
	// 输入流
	private InputStream inputStream;

	// 老师上传课件
	public String uploadCourseware() throws Exception {
		// TODO Auto-generated method stub
		uploadFile("D:\\temp\\", "teacher");
		return "success";
	}

	// 学生上传作业
	public String uploadHomework() {
		ActionContext.getContext().getSession().put("user", "student");
		uploadFile("D:\\homework\\", "student");
		return SUCCESS;
	}

	// 学生得到课件的列表
	public String getCourseware() {
		List<Resource> list = studentService.getCourseware();
		// System.out.println("list:" + list.size() + "--" + list.toString());
		Map coursewareMap = (Map) ActionContext.getContext().get("request");
		coursewareMap.put("CoursewareList", list);
		return "showCoursewareList";
	}

	// 得到作业的列表
	public String getHomework() {
		List<Resource> list = teacherService.getHomeworks();
		// System.out.println("list:" + list.size() + "--" + list.toString());
		Map coursewareMap = (Map) ActionContext.getContext().get("request");
		coursewareMap.put("HomeworkList", list);
		return "showHomeworkList";
	}

	// 删除课件
	public String deleteCourseware() {
		teacherService.deleteCourseware(coursewareId);
		return getCourseware();
	}

	// 得到流,给用户下载
	public InputStream getInputStream() {
		try {
			System.out.println("role=="+ActionContext.getContext().getSession().get("user").toString());
			if ((ActionContext.getContext().getSession().get("user").toString()).equals("student"))
				inputStream = new FileInputStream("D:\\temp\\" + fileName);
			if ((ActionContext.getContext().getSession().get("user").toString()).equals("teacher"))
				inputStream = new FileInputStream("D:\\homework\\" + fileName);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			System.err.println("文件没有找到");
		}

		if (fileName != null) {
			try {
				fileName = URLEncoder.encode(fileName, "UTF-8");
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				System.out.println("转码失败!!!");
				e.printStackTrace();
			}
		}
		if (inputStream == null) {
			System.out.println("getResource error!");
		}
		System.out.println("FileName : " + fileName);
		return inputStream;
	}

	@Override
	public String execute() throws Exception {
		System.out.println("fileName==" + fileName);
		return SUCCESS;
	}

	// 上传文件公共代码
	public void uploadFile(String path, String user) {
		for (int i = 0; i < upload.size(); i++) {
			String savePath = path + uploadFileName.get(i);
			FileOutputStream fos = null;
			FileInputStream fis = null;
			try {
				fos = new FileOutputStream(savePath);
				fis = new FileInputStream(upload.get(i));
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally {
				try {
					fos.close();
					fis.close();
					upload.get(i).delete();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			resource = new Resource();
			resource.setName(uploadFileName.get(i));
			resource.setPath(savePath);
			resource.setUploadDate(new Date());
			resource.setDownloadTimes(0);
			if (user.equals("teacher")) {
				resource.setType(COURSEWARE);
				teacherService.uploadCourseware(resource);
			} else if (user.equals("student")) {
				resource.setType(HOMEWORK);
				studentService.uploadHomework(resource);
			}
		}
	}

	public long getMaximumSize() {
		return maximumSize;
	}

	public void setMaximumSize(long maximumSize) {
		this.maximumSize = maximumSize;
	}

	public String getAllowedTypes() {
		return allowedTypes;
	}

	public void setAllowedTypes(String allowedTypes) {
		this.allowedTypes = allowedTypes;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public StudentService getStudentService() {
		return studentService;
	}

	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileName() {
		return this.fileName;
	}

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public Integer getCoursewareId() {
		return coursewareId;
	}

	public void setCoursewareId(Integer coursewareId) {
		this.coursewareId = coursewareId;
	}

}

struts.xml

	<!-- 资源相关 -->
	<package name="resource" namespace="/resource" extends="struts-default">

		<action name="upload*Action" class="resourceAction" method="upload{1}">
			<!-- 文件拦截器 -->
			<interceptor-ref name="fileUpload">
				<!-- 单个文件的大小 -->
				<param name="maximumSize ">10000000</param>
				<param name="allowedTypes">application/octet-stream,application/vnd.ms-powerpoint,application/msword</param>
			</interceptor-ref>
			<interceptor-ref name="defaultStack"></interceptor-ref>

			<result name="success" type="redirect">/index.jsp</result>
			<result name="input">/error.jsp</result>
			<result name="error">/error.jsp</result>
		</action>

		<action name="downloadAction" class="resourceAction">
			<!-- 设置一个stream类型的result -->
			<result name="success" type="stream">
				<param name="contentType">application/octet-stream,application/vnd.ms-powerpoint,application/msword</param>
				<param name="contentDisposition">attachment;filename="${fileName}"</param>
				<param name="inputName">inputStream</param>
				<param name="buffserSize">4096</param>
			</result>
		</action>

		<action name="*Action" class="resourceAction" method="{1}">
			<result name="showCoursewareList">/showCoursewareList.jsp</result>
			<result name="showHomeworkList">/showHomeworkList.jsp</result>
		</action>
	</package>




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值