1.文件上传
(1)请求方法必须是post
(2)enctype的属性值必须为multipart/form-data
(3)提供一个文件选择域
2、文件上传jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<!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>Insert title here</title>
</head>
<body>
文件上传....
<form action="${pageContext.request.contextPath}/sy/UpAction_upLoad.action" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传"/>
</form>
</body>
</html>
3、文件上传编写动作类注意:
(1)struts2提供的上传文件名命名规则:上传的字段名(form表单中的)+fileName
(2)struts2提供MIME类型命名规则:上传的字段名(form表单中的)+contentType
(3)没有使用模型驱动时:form表单提供的属性和struts2提供的属性要生成set和get方法
public class FileUpAction extends DivBaseAction{
//这是从jsp传递过来的具体文r
private File file;
//文件路径
private String fileFileName;
//文件类型
private String fileContentType;
//虚拟路径 相对性
private String serverDir="/upload";
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
/**
* 上传图片到服务器
* 这里为了简单就只保存到了本地的虚拟路径了
* @return
*/
public String upLoad() {
System.out.println("fileFileName:"+fileFileName);
System.out.println("fileContentType:"+fileContentType);
String pathname=getRealPath(serverDir+"/"+fileFileName);
try {
//这里是struts.io 包中自带的方法 保存图片
FileUtils.copyFile(file, new File(pathname));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
/**
* 拿到服务器的真实路径 >>>文件夹
*/
private String getRealPath(String path) {
return this.req.getServletContext().getRealPath(path);
}
}
6、struts.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="base" namespace="/sy">
<!-- !!! -->
<action name="UpAction_*" method="{1}" class="com.zking.action.FileUpAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
二、文件下载
2、文件下载的动作类代码
/**
* 下载图片
* @return
*/
public String saveAs() {
String fileFileName="x7.jpg";
String fileContentType="image/jpg";
resp.setContentType(fileContentType);
resp.setHeader("Content-Disposition", "attachment;filename="+fileFileName);
String pathname=getRealPath(serverDir+"/"+fileFileName);
try {
BufferedInputStream in=new BufferedInputStream(new FileInputStream(new File(pathname)));
BufferedOutputStream out=new BufferedOutputStream(resp.getOutputStream());
copyBufStram(in,out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 自定义下载图片
* @param in
* @param out
* @throws IOException
*/
private void copyBufStram(BufferedInputStream in, BufferedOutputStream out) throws IOException {
byte[] buf=new byte[1024];
int len=0;
while((len=in.read(buf))!=-1) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
3.打开图片
/**
* 在本页面打开图片
* @return
*/
public String openAs() {
String fileFileName="x7.jpg";
String fileContentType="image/jpg";
resp.setContentType(fileContentType);
resp.setHeader("Content-Disposition", "attachment;filename="+fileFileName);
String pathname=getRealPath(serverDir+"/"+fileFileName);
try {
FileUtils.copyFile(new File(pathname), resp.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
有另外一篇分享文章地址是:>https://blog.csdn.net/shaonianbz/article/details/79116871