action访问不了的原因,STRUTS2附件上传和下载,PPT,struts.xml,视频笔记【诗书画唱】
个人的总结&本期导读
enctype="multipart/form-data",文件上传和下载的PPT,注意,视频笔记,附件上传不是上传到本地,eclipse,而是上传到tomcat服务器上。每次启动tomcat服务器的时候都是会把项目都发送到tomcat服务器中。当你的项目上线的时候,是可以脱离eclipse的。
。修改附件上传的文件名为时间戳等的方法的关键之处,附件上传的服务器的访问路径中需要注意的内容。附件上传图片。个人的理解:附件上传时在QQ浏览器等中显示才效果好,在eclipse中显示会出问题,可能是eclipse中的显示有些太低配了。

(如果struts.xml的内容不对,action是会访问不了的,会报404。如果直接复制项目内容到另一个同名项目就会struts.xml,要把struts.xml复制到src下,覆盖原来的struts.xml。)
附件上传图片 START




附件上传图片 END
文件上传和下载的PPT START



创建表单页面,注意必须有enctype="multipart/form-data"配置。文件上传控件的name属性必须存在。

创建action类,假设表单中的type控件的name为upload,则在action中添加File类型的upload属性和String类型的uploadFileName属性以及他们的getter和setter。
注意:红色部分必须一致,即若type控件的name属性变为abc,则后面的两个属性名也必须变为abc和abcFileName。






请实现功能:
1、当上传的图片成功时,跳转到result.jsp页面后能够马上在页面中展示这个图片。
2、提供一个下载页面,展示可以下载的文件列表,当点击文件名时可以直接下载到该文件。
文件上传和下载的PPT END
讲义 START

在STRUTS2框架中实现附件上传
将你的电脑上的文件发送到服务器tomcat上的过程,是客户端电脑和服务器之间的文件传输
将eclipse中的class文件以及样式html文件发布到tomcat服务器上,然后启动tomcat服务器,才可以访问项目中的页面和程序
http://localhost:8888/J190802/img/2.png
讲义 END
例子 START

package com.jy.action;
import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
public class DownloadAction {
private String fileName;//需要下载的文件名
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
//返回InputStream类型的方法名必须以get开头,
//get后面的第一个字母必须大写
public InputStream getDlFile(){
//将需要下载的文件转换成了流
InputStream ins = ServletActionContext.getServletContext()
.getResourceAsStream(fileName);
return ins;
}
public String download(){
return "success";
}
}


package com.jy.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.struts2.ServletActionContext;
public class UploadAction {
//File类型的变量名和type="file"的控件的name属性保持一致
private File up;//选中的需要上传的文件对象
//type="file"的控件的name属性+FileName
private String upFileName;//选中的需要上传的文件名
private String imgPath;//上传的图片在服务器上的url路径
public File getUp() {
return up;
}
public void setUp(File up) {
this.up = up;
}
public String getUpFileName() {
return upFileName;
}
public void setUpFileName(String upFileName) {
this.upFileName = upFileName;
}
public String getImgPath() {
return imgPath;
}
public void setImgPath(String imgPath) {
this.imgPath = imgPath;
}
public String upload(){
//获取服务器的路径
String realPath = ServletActionContext
.getServletContext().getRealPath("");
//项目所在服务器的路径
//E:\JAVA\apache-tomcat-7.0.56\webapps\J190802
System.out.println(realPath);
//设置将上传的文件保存在服务器的哪个目录下
String fullPath = realPath + "/img";
//E:\JAVA\apache-tomcat-7.0.56\webapps\J190802/img
System.out.println("将选中的文件上传到的位置是:" + fullPath);
//上传图片在服务器上的访问路径
imgPath = "http://localhost:8080/J190802/img/" + upFileName;
//检查上面的文件夹是否存在
File file = new File(fullPath);
if(! file.exists()) {//如果这个文件夹不存在,我就创建它
file.mkdir();
}
FileInputStream fis = null;
FileOutputStream fos = null;
try {
//将up文件拷贝到file文件夹下
//将选中的文件转换成Input流
fis = new FileInputStream(up);
//设置文件上传的路径
fos = new FileOutputStream(fullPath + "/" + upFileName);
//进行拷贝
byte[]buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) > 0) {
fos.write(buffer);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fos.close();
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "success";
}
}


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="file" namespace="/fl" extends="struts-default">
<action name="uploadAc" class="com.jy.action.UploadAction"
method="upload">
<result>/result.jsp</result>
</action>
<action name="downloadAc" class="com.jy.action.DownloadAction"
method="download">
<result type="stream">
<param name="contentType">text/plain</param>
<param name="contentDisposition">
<!-- fileName就是DownloadAction的一个属性,表示需要下载的文件名 -->
attachment;fileName="${fileName}"
</param>
<!-- 找到action中的返回值为InputStream的方法,
将get去掉,将首字母改成小写填入到inputName中 -->
<param name="inputName">dlFile</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>


<?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" id="WebApp_ID" version="3.0">
<display-name>J190802</display-name>
<!-- struts2框架的配置 -->
<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>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>



<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=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">
<style type="text/css">
*{
font-size: 50px;
}
</style>
<script type="text/javascript">
//文件名不要写中文
function downloadFile(fileName){
fileName = 'doc/' + fileName;
location.hreff = 'fl/downloadAc.action?fileName=' + fileName;
}
</script>
</head>
<body>
<a hreff="javascript:downloadFile('g190802.xlsx');">下载</a>
</body>
</html>






<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=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">
</head>
<body>
<h1>附件上传成功</h1>
<img srcc="${imgPath }" />
</body>
</html>


<%@ page language="java" contentType="text/html; charset=UTF-8" 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 hreff="<%=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">
<style type="text/css">
*{
font-size: 50px;
}
</style>
</head>
<body>
<form action="fl/uploadAc.action" method="post" enctype="multipart/form-data">
<label>账号:</label><input type="text" name="act" />
<br>
<input type="file" name="up" />
<input type="submit" value="上传" />
</form>
</body>
</html>



个人的理解:可能是eclipse中的显示有些太低配了。
例子 END
视频笔记 START


附件上传不是上传到本地,eclipse,而是上传到tomcat服务器上。

每次启动tomcat服务器的时候都是会把项目都发送到tomcat服务器中。


修改附件上传的文件名为时间戳等的方法的关键之处 START

修改附件上传的文件名为时间戳等的方法的关键之处 END
附件上传的服务器的访问路径中需要注意的内容 START

附件上传的服务器的访问路径中需要注意的内容 END