重点:
1.文件上传的三种方案;
2.文件上传、下载、查看
重点解析:
-
1、文件上传三种方式:
1.1、将文件以二进制的形式保存到数据库中 activiti工作流框架
1.2、将文件存储到专门文件服务器(存放文件用的Linux系统)中
1.3、直接将文件存储到服务器(tomcat所在服务器)中 -
2.文件(实例:图片)
2.1上传实例:
前面设置的三个属性必须要提供set、get方法,要不然文件上传不了
jsp界面的代码:
<h1> struts的上传</h1>
<form action="${pageContext.request.contextPath }/sy/uploadAction_upload.action" enctype="multipart/form-data" method="post">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
2.2、文件另存
这个方法跟上面是一个class里面的在,在class文件内我把类型(type)和名称(name)写死了,为了测试,属性和名称可以改成动态的
jsp代码:
<s:url var="saveAsUrl" namespace="/sy" action="uploadAction_saveAs"></s:url>
<s:a href="%{#saveAsUrl}">保存图片</s:a>
2.3 文件显示
其实文件显示和文件另存方法是类似的
jsp代码:
<h2>直接展示</h2>
<s:url var="openAsUrl" namespace="/sy" action="uploadAction_openAs"></s:url>
其实struts2的文件上传比之前的学的文件上传要容易,代码也简化了许多
struts2的一些其他内容分享
-
内容类型
response.setContentType(d.getMime()); -
设置响应头
response.setHeader(“Content-Disposition”,“attachment;filename=” + fileName);//文件名 -
处理文件名的中文乱码
String fileName = d.getFileName();
fileName = new String(fileName.getBytes(“utf-8”), “iso8859-1”); -
struts2文件上传大小设置
-
struts2文件上传类型设置
image/png,image/gif,image/jpeg
根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制