基于struts2框架文件的上传与下载
在开发一些社交网站时,需要有允许用户上传自己本地文件的功能,则需要文件的上传下载代码。
首先考虑的是文件的储存位置,这里不考虑存在数据库,因为通过数据库查询获取十分消耗资源与时间,故需将数据存储在服务器上。
1. 前台页面的代码:
<form action="photo-up" method="post" enctype="multipart/form-data">
上传图片: <input type="file" name="myFile">
<!--或者<s:file name="myFile" label="FILE"/> --> <input type="submit" value="上传"/><s:fielderror id="error"/>
</form>
<s:file name="myFile" label="FILE"/>
上面的重要属性是name,要求如后台的文件定义名一样。
2. 文件上传后台代码(struts2的Action控制类):
public class PhotoAction extends ActionSupport {
private static final long serialVersionUID = 6897968438458946989L;
private File myFile;
private String myFileContentType;
private String myFileFileName;
//定义以上三个变量,myFile是接受前台的文件,要求名字与name属性一样,其他的见名知意(就当固定格式)
User user = (User)ActionContext.getContext().getSession().get("user") ;
public String up() throws Exception {
//创建输入流
InputStream is = new FileInputStream(myFile) ;
//设置不同用户的文件保存目录
String photoPath = ServletActionContext.getServletContext()
.getRealPath("/user/photo/" + user.getUserName()) ;
File filePhotoPath = new File(photoPath);
if(!filePhotoPath.isDirectory()) {
filePhotoPath.mkdir();
}
//解决中文文件名问题,采用UUID获取随机字符串
String extension = FilenameUtils.getExtension(getMyFileFileName());//获取文件的拓展名
String filename = UUID.randomUUID().toString() + "."+ extension;
File toFile = new File(filePhotoPath, filename) ;
//输出流
OutputStream os = new FileOutputStream(toFile) ;
byte[] buf = new byte[1024] ;
int lenth ;
while((lenth = is.read(buf)) >0 ){
os.write(buf, 0, lenth);
}
is.close();
os.close();
return "up";
}
//属性的get、set方法
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
}
此代码将文件存储在 (Web应用程序的根目录的绝对路径)/user/photo/(userName) 下面。
在struts.xml文件中配置文件限制要求:
<action name="photo-*" class="photoAction" method="{1}">
<!-- 配置fileUpload拦截器 -->
<interceptor-ref name="fileUpload">
<!-- 配置允许上传文件类型 -->
<param name="allowedTypes">image/bmp,image/pjpeg,image/gif,image/png</param>
<!-- 配置允许上传文件大小最大值 -->
<param name="maximumSize">512000</param>
</interceptor-ref>
<result name="up" type="redirectAction">photo-showPhoto</result>
<result name="input" type="redirectAction">photo-showPhoto</result>
<result name="showPhoto">/showphoto.jsp</result>
</action>
3. 文件的下载代码
文件的下载代码比较简单,只需要得到web应用程序目录下文件的所在位置即可,然后放在一个超链接里面点击就可以浏览或下载。
public String showPhoto(){
String photoPath = ServletActionContext.getServletContext()
.getRealPath("/user/photo/" + user.getUserName()) ;
File file = new File(photoPath) ;
//获取该目录下所有的文件名
String[] photoList = file.list() ;
//将获取文件名的集合放在request里
ServletActionContext.getRequest().setAttribute("photoList", photoList);
return "showPhoto" ;
}
4. 返回页面,显示文件连接:
<table align="center" border="1" cellpadding="0" cellspacing="10">
<tr>
<s:iterator value="#request.photoList" var="photo" status="stu">
<td>
<a href='user/photo/${sessionScope.user.userName }/<s:property value="photo"/>'>
<img alt="" src='user/photo/${sessionScope.user.userName }/<s:property value="photo"/>'
width="100" height="120"></a>
</td>
<s:if test="(#stu.index + 1) % 6 == 0">
</tr>
<tr>
</s:if>
</s:iterator>
</tr>
</table>
以上有个在table里自动换行的小技巧,当(集合下标+1)能整除6就加</tr>结束一行,加<tr>开始新的一行。
基于struts2框架文件的上传与下载的更多相关文章
- Struts2控制文件的上传与下载
Struts2控制文件上传与下载的几个注意事项: (1)必须将表单的method设置为post,将enctype设置为multipart/from-data.只有这样,浏览器才会把用户选择文件的二进制 ...
- 使用Struts2实现文件的上传和下载
(一)单个文件的上传步骤: 1.拷贝jar包:commons-fileupload.jar, commons-io.jar 下载链接(文件上传.rar):http://www.cnblogs.com ...
- struts2实现文件的上传和下载实例[转]
实现原理 Struts 2是通过Commons FileUpload文件上传. Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器 ...
- Struts2中文件的上传与下载
文件上传 1.jsp页面 <s:form action="fileAction" namespace="/file" method="POST& ...
- SpringMVC框架——文件的上传与下载
使用SpringMVC框架做个小练习,需求: 1.单个图片上传并显示到页面中: 2.多个图片上传并显示到页面中: 3.上传文件后下载文件: 1.pom.xml中添加依赖 <!-- 文件上传 -- ...
- JSP文件的上传和下载
文件上传下载,与传统的方式不同,这里能够上传和下载10G以上的文件.而且支持断点续传. 通常情况下,我们在网站上面下载的时候都是单个文件下载,但是在实际的业务场景中,我们经常会遇到客户需要批量下载的场 ...
- Struts2实现文件的上传与动态下载功能。
本篇主要使用Struts2实现文件的上传与动态下载功能.出于安全考虑,所以,在硬盘上存储上传的文件的时候,统一都重新命名为随机字符串.用数据库存储真实文件名与随机文件名称之间的关联. 下面的是实体类 ...
- 在SpringMVC框架下实现文件的 上传和 下载
在eclipse中的javaEE环境下:导入必要的架包 web.xml的配置文件: <?xml version="1.0" encoding="UTF-8" ...
- Gin-Go学习笔记四:Gin-Web框架 文件的上传下载
文件的上传和下载 1->文件的上传 文件的上传,采用的是uploadify.js这个插件. 本事例实现的是上传图片文件,其他的文件上传也一样. 2->文件的下载 文件的下载有两个实现的方式 ...
随机推荐
- vue的全家桶
自定义指令 <div v-demo="{ color: 'white', text: 'hello!' }"></div> Vue.directive('d ...
- /dev/null 和 /dev/zero
1.概论 -- 来自维基的解释 /dev/null : 在类Unix系统中,/dev/null,或称空设备,是一个特殊的设备文件,它丢弃一切写入其中的数据(但报告写入操作成功),读取它则会立即得到一 ...
- 首次使用JBoss流程(windows下)
1.首先应该明白JBoss分为社区版(AS)和企业版(EAP),其中社区版已经改名wildfly(难道是野苍蝇的意思?),企业版对个人开发者免费下载使用, 这里由于公司要求,我用的是jboss-eap ...
- 【apache】apache模拟高并发请求
目的:测试程序的性能 运用的工具是apache的ab工具,装有apache服务器的一般都有ab工具. lamp命令: ab -c 10 -n 100 "http://a.ilanni.com ...
- tp请求和响应
一.请求参数 use think\Request; 1.获取方法如下: http://w.tp.com/index/index/index/user/AAA $this->request-> ...
- LINQ to SQL语句之Select/Distinct和Count/Sum/Min/Max/Avg (转)
Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...
- GOF对Builder模式的定义(转载)
(1)意图 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. (2)适用性 1. 当创建复杂对象的算法应该独立于该对象的组成部分以及他们的装配方式:2. 当构造过程必须允许构 ...
- 安卓API首页
http://developer.android.com/reference/packages.html
- KVM部署LVS集群故障案例一则
一.故障现象 KVM部署LVS(Linux Virtual Server)集群后,能够单独以HTTP方式访问RS(Real Server)的实际IP,但无法通过VIP(Virtual IP)访问. 二 ...
- 基于注解的ssh框架之spring配置文件
<?xml version="1.0" encoding="UTF-8"?> com.mysql.jdbc.Driver jdbc:mysql:// ...