S10.1_Struts2_FileUpload_Download 文件上传与下载
2017-01-22 14:10
519 查看
我们接下来将要创建的项目目录结构如下:
net.nw.action
package net.nw.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String downloadFile;
private String downloadFile_CN="范例.pdf";
public String getDownloadFile() {
return downloadFile;
}
public void setDownloadFile(String downloadFile) {
this.downloadFile = downloadFile;
}
public String getDownloadFile_CN() {
return downloadFile_CN;
}
public void setDownloadFile_CN(String downloadFile_CN) {
this.downloadFile_CN = downloadFile_CN;
}
public DownloadAction(){
System.out.println("构造方法DownloadAction ......");
}
//调用业务层操作类的方法
public String execute() throws Exception{
System.out.println("打印downloadFile:"+downloadFile);
return SUCCESS;
}
public InputStream getTargetFile() throws Exception{
downloadFile_CN = new String(downloadFile_CN.getBytes(),"iso8859-1");
return ServletActionContext.getServletContext().getResourceAsStream(downloadFile);
}
}
package net.nw.action;
import net.nw.dao.UserDao;
import net.nw.vo.User;
//Action三种实现方式 1.具有public String execute()方法的普通类 <action name="login" class="net.nw.action.loginAction">
public class LoginAction {
private String username;//必须与html标签中的名称一样,此处私有变量的值直接来自html标签对应名称填充过来的值
private String password;//必须与html标签中的名称一样,此处私有变量的值直接来自html标签对应名称填充过来的值
public LoginAction(){
System.out.println("构造方法LoginAction ......");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//调用业务层操作类的方法
public String execute(){
User u = new User();
u.setUsername(this.getUsername());
u.setPassword(this.getPassword());
UserDao dao = new UserDao();
if(dao.userLogin(u)){
return "login_success";
} else{
return "login_failure";
}
}
}
package net.nw.action;
import com.opensymphony.xwork2.Action;
import net.nw.dao.UserDao;
import net.nw.vo.User;
//Action三种实现方式 2.实现Action接口 <action name="login" class="net.nw.action.loginAction2">
public class LoginAction2 implements Action{
private String username;//必须与html标签中的名称一样,此处私有变量的值直接来自html标签对应名称填充过来的值
private String password;//必须与html标签中的名称一样,此处私有变量的值直接来自html标签对应名称填充过来的值
public LoginAction2(){
System.out.println("构造方法LoginAction2 ......");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//调用业务层操作类的方法
public String execute(){
User u = new User();
u.setUsername(this.getUsername());
u.setPassword(this.getPassword());
UserDao dao = new UserDao();
if(dao.userLogin(u)){
return "login_success";
} else{
return "login_failure";
}
}
}
package net.nw.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private File photo;//上传文件
private String photoFileName;//上传文件的文件名
private String photoContentType; //上传文件的文件类型
private String savePath;//上传文件的保存目录
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public UploadAction(){
System.out.println("构造方法UploadAction ......");
}
//调用业务层操作类的方法
public String execute() throws Exception{
System.out.println("执行上传execute() ......");
//1.获取上传后文件的真实目录
String path = ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(path);
//2.获得上传文件的真实路径
String f_name = path + File.separator + this.getPhotoFileName();
System.out.println(f_name);
//3.创建输入和输出流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(photo));
bos = new BufferedOutputStream(new FileOutputStream(new File(f_name)));
byte[] buff = new byte[(int)photo.length()];
int len=0;
while((len=bis.read(buff))!=-1){
bos.write(buff,0,len);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(bis!=null){
bis.close();
bis = null;
}
if(bos!=null){
bos.close();
bos = null;
}
}catch(Exception ex){
ex.printStackTrace();
}
}
return SUCCESS;
}
}
package net.nw.dao;
import net.nw.vo.User;
//用户操作类
public class UserDao {
public boolean userLogin(User u){
if(!u.getUsername().equals("") && u.getPassword().equals("123456")){
return true;
} else {
return false;
}
}
}
package net.nw.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.nw.dao.UserDao;
import net.nw.vo.User;
/**
* Servlet implementation class loginServlet
*/
public class loginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public loginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
User u = new User();
u.setUsername(request.getParameter("username"));
u.setPassword(request.getParameter("password"));
UserDao dao = new UserDao();
if(dao.userLogin(u)){
response.sendRedirect("login_success.jsp");
}else{
response.sendRedirect("login_failure.jsp");
}
}
}
package net.nw.vo;
//用户类
public class User {
private String username;//用户名称
private String password;//用户密码
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?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" ></constant>
<constant name="struts.i18n.encoding" value="utf-8" ></constant>
<constant name="struts.multipart.maxSize" value="31457280" ></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="net.nw.action.UploadAction">
<param name="savePath">upload</param>
<result name="success">/upload_success.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">
31457280
</param>
<param name="fileUpload.allowedTypes">
image/gif,image/jpeg,application/msword,application/pdf,application/octet-stream
</param>
</interceptor-ref>
</action>
<action name="download" class="net.nw.action.DownloadAction">
<!--<result name="success">/download_success.jsp</result>-->
<result name="success" type="stream">
<param name="inputName">targetFile</param>
<param name="contentDisposition">attachment;filename="${downloadFile_CN}"</param>
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
</result>
</action>
</package>
</struts>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
<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>
download_success.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<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>
<center>
<h1>下载成功</h1>
<hr>
</center>
</body>
</html>
upload_success.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<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>
<center>
<h1>上传成功</h1>
<hr>
<s:url var="photo_url" value="view_photo.jsp">
<s:param name="filename" value="%{savePath+'/'+photoFileName}"/>
</s:url>
<s:property value="%{savePath+'/'+photoFileName}"/><br>
<s:a href="%{#photo_url}">查看</s:a>
<br>
<br>
<s:url var="down_url" value="download.action">
<s:param name="downloadFile" value="%{savePath+'/'+photoFileName}"/>
</s:url>
<s:a href="%{#down_url}">下载</s:a>
</center>
</body>
</html>
upload.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<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>
<center>
<h1>文件上传</h1>
<hr>
<s:fielderror/>
<s:form name="testForm" action="upload" method="post" enctype="multipart/form-data">
<s:file name="photo" label="%{'请上传文件'}"></s:file>
<s:submit value="提交"/>
</s:form>
</center>
</body>
</html>
view_photo.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<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>
<center>
<h1>查看图片</h1>
<hr>
<img src="${param.filename}"/>
<a href="<%=path%>/upload_success.jsp">返回</a>
</center>
</body>
</html>
测试效果截图如下:
本项目下载地址:点击打开链接
net.nw.action
package net.nw.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class DownloadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private String downloadFile;
private String downloadFile_CN="范例.pdf";
public String getDownloadFile() {
return downloadFile;
}
public void setDownloadFile(String downloadFile) {
this.downloadFile = downloadFile;
}
public String getDownloadFile_CN() {
return downloadFile_CN;
}
public void setDownloadFile_CN(String downloadFile_CN) {
this.downloadFile_CN = downloadFile_CN;
}
public DownloadAction(){
System.out.println("构造方法DownloadAction ......");
}
//调用业务层操作类的方法
public String execute() throws Exception{
System.out.println("打印downloadFile:"+downloadFile);
return SUCCESS;
}
public InputStream getTargetFile() throws Exception{
downloadFile_CN = new String(downloadFile_CN.getBytes(),"iso8859-1");
return ServletActionContext.getServletContext().getResourceAsStream(downloadFile);
}
}
package net.nw.action;
import net.nw.dao.UserDao;
import net.nw.vo.User;
//Action三种实现方式 1.具有public String execute()方法的普通类 <action name="login" class="net.nw.action.loginAction">
public class LoginAction {
private String username;//必须与html标签中的名称一样,此处私有变量的值直接来自html标签对应名称填充过来的值
private String password;//必须与html标签中的名称一样,此处私有变量的值直接来自html标签对应名称填充过来的值
public LoginAction(){
System.out.println("构造方法LoginAction ......");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//调用业务层操作类的方法
public String execute(){
User u = new User();
u.setUsername(this.getUsername());
u.setPassword(this.getPassword());
UserDao dao = new UserDao();
if(dao.userLogin(u)){
return "login_success";
} else{
return "login_failure";
}
}
}
package net.nw.action;
import com.opensymphony.xwork2.Action;
import net.nw.dao.UserDao;
import net.nw.vo.User;
//Action三种实现方式 2.实现Action接口 <action name="login" class="net.nw.action.loginAction2">
public class LoginAction2 implements Action{
private String username;//必须与html标签中的名称一样,此处私有变量的值直接来自html标签对应名称填充过来的值
private String password;//必须与html标签中的名称一样,此处私有变量的值直接来自html标签对应名称填充过来的值
public LoginAction2(){
System.out.println("构造方法LoginAction2 ......");
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
//调用业务层操作类的方法
public String execute(){
User u = new User();
u.setUsername(this.getUsername());
u.setPassword(this.getPassword());
UserDao dao = new UserDao();
if(dao.userLogin(u)){
return "login_success";
} else{
return "login_failure";
}
}
}
package net.nw.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private File photo;//上传文件
private String photoFileName;//上传文件的文件名
private String photoContentType; //上传文件的文件类型
private String savePath;//上传文件的保存目录
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public UploadAction(){
System.out.println("构造方法UploadAction ......");
}
//调用业务层操作类的方法
public String execute() throws Exception{
System.out.println("执行上传execute() ......");
//1.获取上传后文件的真实目录
String path = ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(path);
//2.获得上传文件的真实路径
String f_name = path + File.separator + this.getPhotoFileName();
System.out.println(f_name);
//3.创建输入和输出流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
bis = new BufferedInputStream(new FileInputStream(photo));
bos = new BufferedOutputStream(new FileOutputStream(new File(f_name)));
byte[] buff = new byte[(int)photo.length()];
int len=0;
while((len=bis.read(buff))!=-1){
bos.write(buff,0,len);
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
try{
if(bis!=null){
bis.close();
bis = null;
}
if(bos!=null){
bos.close();
bos = null;
}
}catch(Exception ex){
ex.printStackTrace();
}
}
return SUCCESS;
}
}
package net.nw.dao;
import net.nw.vo.User;
//用户操作类
public class UserDao {
public boolean userLogin(User u){
if(!u.getUsername().equals("") && u.getPassword().equals("123456")){
return true;
} else {
return false;
}
}
}
package net.nw.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.nw.dao.UserDao;
import net.nw.vo.User;
/**
* Servlet implementation class loginServlet
*/
public class loginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public loginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
User u = new User();
u.setUsername(request.getParameter("username"));
u.setPassword(request.getParameter("password"));
UserDao dao = new UserDao();
if(dao.userLogin(u)){
response.sendRedirect("login_success.jsp");
}else{
response.sendRedirect("login_failure.jsp");
}
}
}
package net.nw.vo;
//用户类
public class User {
private String username;//用户名称
private String password;//用户密码
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<?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" ></constant>
<constant name="struts.i18n.encoding" value="utf-8" ></constant>
<constant name="struts.multipart.maxSize" value="31457280" ></constant>
<package name="default" namespace="/" extends="struts-default">
<action name="upload" class="net.nw.action.UploadAction">
<param name="savePath">upload</param>
<result name="success">/upload_success.jsp</result>
<result name="input">/upload.jsp</result>
<interceptor-ref name="defaultStack">
<param name="fileUpload.maximumSize">
31457280
</param>
<param name="fileUpload.allowedTypes">
image/gif,image/jpeg,application/msword,application/pdf,application/octet-stream
</param>
</interceptor-ref>
</action>
<action name="download" class="net.nw.action.DownloadAction">
<!--<result name="success">/download_success.jsp</result>-->
<result name="success" type="stream">
<param name="inputName">targetFile</param>
<param name="contentDisposition">attachment;filename="${downloadFile_CN}"</param>
<param name="contentType">application/octet-stream;charset=ISO8859-1</param>
</result>
</action>
</package>
</struts>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>upload.jsp</welcome-file>
</welcome-file-list>
<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>
download_success.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<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>
<center>
<h1>下载成功</h1>
<hr>
</center>
</body>
</html>
upload_success.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<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>
<center>
<h1>上传成功</h1>
<hr>
<s:url var="photo_url" value="view_photo.jsp">
<s:param name="filename" value="%{savePath+'/'+photoFileName}"/>
</s:url>
<s:property value="%{savePath+'/'+photoFileName}"/><br>
<s:a href="%{#photo_url}">查看</s:a>
<br>
<br>
<s:url var="down_url" value="download.action">
<s:param name="downloadFile" value="%{savePath+'/'+photoFileName}"/>
</s:url>
<s:a href="%{#down_url}">下载</s:a>
</center>
</body>
</html>
upload.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<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>
<center>
<h1>文件上传</h1>
<hr>
<s:fielderror/>
<s:form name="testForm" action="upload" method="post" enctype="multipart/form-data">
<s:file name="photo" label="%{'请上传文件'}"></s:file>
<s:submit value="提交"/>
</s:form>
</center>
</body>
</html>
view_photo.jsp:
<%@ page language="java" import="java.util.*" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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">
<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>
<center>
<h1>查看图片</h1>
<hr>
<img src="${param.filename}"/>
<a href="<%=path%>/upload_success.jsp">返回</a>
</center>
</body>
</html>
测试效果截图如下:
本项目下载地址:点击打开链接
相关文章推荐
- 文件的上传和下载------FileUpload与DownLoad
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- [导入]Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- struts2文件上传下载及乱码问题解决
- Struts2中的文件上传下载
- 使用fileupload上传文件,并提供下载
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Struts2通过commons-fileupload实现文件上传
- Struts2学习笔记15:Struts2的文件上传和下载【续】二
- Struts2学习笔记13:Struts2的文件上传和下载
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载).NET技术
- struts2 common-fileupload上传文件大小限制
- Struts2文件的上传和下载
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Struts2文件的上传和下载
- commons-FileUpload文件上传下载,及中文乱码、IE7路径错误问题解决
- Struts2中的Blob格式文件上传下载