- 浏览: 682053 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (362)
- java基础 (33)
- html/css+div/javascript (17)
- Ajax/XML/JSON/XSL (7)
- JQuery (11)
- Extjs (1)
- JSP/Servlet (19)
- MVC模式 (4)
- struts 1 (17)
- Struts 2.3.4 (17)
- Spring 3.2 (26)
- Springmvc (3)
- Hibernate 4.1 (21)
- ibatis (6)
- Velocity模板语言 (2)
- Rose框架 (5)
- EJB (1)
- JUnit测试 (2)
- 数据库DB (24)
- 重构 / 设计模式 (3)
- 开发工具IDE (37)
- 数据结构与算法设计 (3)
- Android (12)
- Linux (4)
- bug集合 (29)
- 缓存技术(redis) (3)
- Lucene全文索引 (15)
- maven3.0.5 (4)
- 小工具集合 (18)
- 面试题 (5)
- 闲聊 (11)
- 其他 (4)
- 接口API (2)
- work (2)
- Flex (0)
- JMS (1)
- 开源项目集合 (1)
- 技术博客 (1)
- 分类04 (0)
- 分类05555 (0)
最新评论
-
小小小羊:
好屌...
java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$Refle -
liubinli2005:
这个可以脱底spring。单独使用吗?
DAO层:jade -
cangbaotu:
我觉得对于开发者来说,能脚本化编写爬虫是一件挺开心的事情( ̄▽ ...
网页爬取 -
asjava:
很好的文章, 但每段代码清单都重复了一次.
spring 事务 -
xia635317478:
jethypc 写道验证码的session无法传过去啊 还是我 ...
登陆验证码(struts2实现)
上传功能的实现(Struts2)
在WebRoot根目录新建一个images文件夹
upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <html> <head> <base href="<%=basePath%>"> </head> <body bgcolor="green"> <s:form name="" action="momo/upload" enctype="multipart/form-data"> 图片路径:<s:file name="myFile" label="浏览照片"/> <s:submit value="上传"/> </s:form> </body> </html>
struts.xml
<?xml version="1.0" encoding="GBK" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.i18n.encoding" value="gbk"/> <constant name="struts.custom.i18n.resources" value="ApplicationResources"/> <package name="mm" extends="struts-default" namespace="/momo"> <action name="upload" class="com.org.upload.action.UploadAction"> <interceptor-ref name="defaultStack"> <param name="fileUpload.maximumSize">10240000</param> <!-- 1024b=1Kb (10000KB/1024)M --> <param name="fileUpload.allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg,image/pjpeg, application/msword,text/plain,application/vnd.ms-excel <!-- .doc .txt .xls --> </param> </interceptor-ref> <result name="input">/upload.jsp</result> <result name="success">/success.jsp</result> <param name="savePath">/images</param> </action> </package> </struts>
UploadAction.java
package com.org.upload.action; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.sql.Timestamp; import java.util.Date; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action; public class UploadAction implements Action{ private File myFile; //上传文件的file对象 private String myFileFileName;//上传文件名 private String myFileContentType;//上传文件的MINE类型 jpeg/jpg private String savePath;//保存路径 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; } public String getSavePath() { return savePath; } public void setSavePath(String savePath) { this.savePath = savePath; } public String execute() throws Exception{ String newFileName=null; //得到当前时间自1900年1月7日0分0秒开始流逝的毫秒数,将这个毫秒数作为上传文件的文件名 String now = ""+(1900+new Date().getYear()) + (1+new Date().getMonth())+ (7+new Date().getDay()) + new Date().getHours()+new Date().getMinutes()+new Date().getSeconds() ; System.out.print("年"+(1900+new Date().getYear())+"/月"+(1+new Date().getMonth())+"/日"+(7+new Date().getDay())+"/时"+new Date().getHours()+"/分"+new Date().getMinutes()+"/秒"+new Date().getSeconds()) ; //得到保存上传文件的目录的真实路径 String path=ServletActionContext.getServletContext().getRealPath(savePath); File dir=new File(path); //如果这个目录不存在,则创建它。 if(!dir.exists()){ dir.mkdir(); } int index=myFileFileName.lastIndexOf("."); //判断上传文件的名是否有扩展名 if(index!=-1){ newFileName=now+myFileFileName.substring(index); }else{ newFileName=now; } BufferedOutputStream bos=null; BufferedInputStream bis=null; //读取保存在临时目录下的上传文件,写入到新的文件中。 FileInputStream fis=new FileInputStream(myFile); bis=new BufferedInputStream(fis); FileOutputStream fos=new FileOutputStream(new File(dir,newFileName)); bos=new BufferedOutputStream(fos); byte[] buf=new byte[4096]; int len=-1; while((len=bis.read(buf))!=-1){ bos.write(buf, 0, len); } bos.flush(); return SUCCESS; } }
上传文件类型和名字:
Content-Type not allowed: file "09poC_wallpapers.jpg" "upload_1ea6fe4e_13611ac7d7c__8000_00000012.tmp" image/pjpeg
.a :
application/octet-stream
.ai :
application/postscript
'.aif' :
'audio/x-aiff',
'.aifc' :
'audio/x-aiff',
'.aiff' :
'audio/x-aiff',
'.au' : '
audio/basic',
'.avi' :
'video/x-msvideo',
'.bat' :
'text/plain',
'.bcpio' :
'application/x-bcpio',
'.bin' :
'application/octet-stream',
'.bmp' :
'image/x-ms-bmp',
'.c' :
'text/plain',
# Duplicates
'.cdf' :
'application/x-cdf',
'.cdf'
: 'application/x-netcdf',
'.cpio' :
'application/x-cpio',
'.csh' :
'application/x-csh',
'.css' :
'text/css',
'.dll' :
'application/octet-stream',
'.doc' :
'application/msword',
'.dot' :
'application/msword',
'.dvi' :
'application/x-dvi',
'.eml' :
'message/rfc822',
'.eps' :
'application/postscript',
'.etx' :
'text/x-setext',
'.exe' :
'application/octet-stream',
'.gif' :
'image/gif',
'.gtar' :
'application/x-gtar',
'.h' :
'text/plain',
'.hdf' :
'application/x-hdf',
'.htm' :
'text/html',
'.html' :
'text/html',
'.ief' :
'image/ief',
'.jpe' :
'image/jpeg',
'.jpeg' :
'image/jpeg',
'.jpg' :
'image/jpeg',
'.js' :
'application/x-javascript',
'.ksh' :
'text/plain',
'.latex' :
'application/x-latex',
'.m1v' :
'video/mpeg',
'.man' :
'application/x-troff-man',
'.me' :
'application/x-troff-me',
'.mht' :
'message/rfc822',
'.mhtml' :
'message/rfc822',
'.mif' :
'application/x-mif',
'.mov' :
'video/quicktime',
'.movie' :
'video/x-sgi-movie',
'.mp2' :
'audio/mpeg',
'.mp3' :
'audio/mpeg',
'.mpa' :
'video/mpeg',
'.mpe' :
'video/mpeg',
'.mpeg' :
'video/mpeg',
'.mpg' :
'video/mpeg',
'.ms' :
'application/x-troff-ms',
'.nc' :
'application/x-netcdf',
'.nws' :
'message/rfc822',
'.o' :
'application/octet-stream',
'.obj' :
'application/octet-stream',
'.oda' :
'application/oda',
'.p12' :
'application/x-pkcs12',
'.p7c' :
'application/pkcs7-mime',
'.pbm' :
'image/x-portable-bitmap',
'.pdf' :
'application/pdf',
'.pfx' :
'application/x-pkcs12',
'.pgm' :
'image/x-portable-graymap',
'.pl' :
'text/plain',
'.png' :
'image/png',
'.pnm' :
'image/x-portable-anymap',
'.pot' :
'application/vnd.ms-powerpoint',
'.ppa' :
'application/vnd.ms-powerpoint',
'.ppm' :
'image/x-portable-pixmap',
'.pps' :
'application/vnd.ms-powerpoint',
'.ppt' :
'application/vnd.ms-powerpoint',
'.ps' :
'application/postscript',
'.pwz' :
'application/vnd.ms-powerpoint',
'.py' :
'text/x-python',
'.pyc' :
'application/x-python-code',
'.pyo' :
'application/x-python-code',
'.qt' :
'video/quicktime',
'.ra' :
'audio/x-pn-realaudio',
'.ram' :
'application/x-pn-realaudio',
'.ras' :
'image/x-cmu-raster',
'.rdf' :
'application/xml',
'.rgb' :
'image/x-rgb',
'.roff' :
'application/x-troff',
'.rtx' :
'text/richtext',
'.sgm' :
'text/x-sgml',
'.sgml' :
'text/x-sgml',
'.sh' :
'application/x-sh',
'.shar' :
'application/x-shar',
'.snd' :
'audio/basic',
'.so' :
'application/octet-stream',
'.src' :
'application/x-wais-source',
'.sv4cpio':
'application/x-sv4cpio',
'.sv4crc' :
'application/x-sv4crc',
'.swf' :
'application/x-shockwave-flash',
'.t' :
'application/x-troff',
'.tar' :
'application/x-tar',
'.tcl' :
'application/x-tcl',
'.tex' :
'application/x-tex',
'.texi' :
'application/x-texinfo',
'.texinfo':
'application/x-texinfo',
'.tif' :
'image/tiff',
'.tiff' :
'image/tiff',
'.tr' :
'application/x-troff',
'.tsv' :
'text/tab-separated-values',
'.txt' :
'text/plain',
'.ustar' :
'application/x-ustar',
'.vcf' :
'text/x-vcard',
'.wav' :
'audio/x-wav',
'.wiz' :
'application/msword',
'.wsdl' :
'application/xml',
'.xbm' :
'image/x-xbitmap',
'.xlb' :
'application/vnd.ms-excel',
# Duplicates
'.xls' :
'application/excel',
'.xls' :
'application/vnd.ms-excel',
.xml :
text/xml
.xpdl:
application/xml
.xpm :
image/x-xpixmap
.xsl :
application/xml
.xwd :
image/x-xwindowdump
.zip :
application/zip
firefox 和 ie 的文件类型区别
Firefox:
image/jpeg, image/bmp, image/gif, image/png
ie 6:
image/pjpeg ,image/bmp, image/gif, image/x-png
ie 7:
image/pjpeg, image/bmp, image/gif, image/x-png
ie 8:
image/pjpeg, image/bmp, image/gif, image/x-png
Ie 9:
image/jpeg, image/bmp, image/gif, image/png
所以在struts.xml配置文件中需要
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg,
image/pjpeg ,image/bmp, image/gif, image/x-png,
</param>
发表评论
-
网页爬取
2013-09-05 13:10 1031网页爬取 参考: 1.http://www.csdnjava ... -
Struts2学习笔记
2013-07-27 10:42 1121Struts2学习笔记 1.官网下载包 URL:http ... -
浏览器插件集合(火狐+谷歌)
2013-07-03 10:26 1161Change Host 引用c:\windows\syste ... -
【32位和64位不同的操作系统】环境的配置
2013-07-02 13:34 1151【32位和64位不同的操作 ... -
跨域共享Cookies
2013-06-29 22:23 12315跨域共享Cookies 正常的cookie只能在一个应用中 ... -
代码托管Eclipse插件--Egit使用(GitHub)
2013-06-09 00:09 4820代码托管Eclipse插件--Egit使用(GitHub) ... -
FindBugs简介
2013-05-14 16:18 1277FindBugs简介 一、介绍 FindBugs 是一个静态 ... -
常用Java代码质量检测评估工具
2013-05-14 16:07 8332常用Java代码质量检测评估工具 1. PMD fro ... -
乱码解决
2013-05-09 16:31 967乱码解决 1.超链接发送参数: 发送的时候使用URLEnc ... -
JAVA操作Excel
2013-05-05 15:02 916JAVA操作Excel JAVA 通常有两种方法来操作Ex ... -
JavaMail邮件发送Demo
2012-12-26 11:37 3048JavaMail邮件发送Demo import com ... -
Spring分别与Struts1.x 和Struts2.x集成方法
2012-11-16 14:05 1467Spring分别与Struts1.x 和Struts2.x集成 ... -
Struts2_Action处理类获取Scope对象
2012-11-16 14:05 500Struts2_Action处理类获取Scope对象 1 ... -
Struts2一个Action内包含多个请求处理方法(三种方式)
2012-11-12 09:29 1145Struts2一个Action内包含多个请求处理方法(三种方式 ... -
struts2中 ServletActionContext与ActionContext区别 .
2012-10-30 17:29 1083http://blog.csdn.net/lushuaiyin ... -
Struts2配置文件详解
2012-10-08 14:33 1000struts2共有4类配置文件, struts.pr ... -
Action处理类
2012-10-08 14:16 854Action处理类 一、可以实现Action接口 ... -
struts2标签02
2012-10-08 13:54 1714struts2标签02 34. property ... -
struts2标签01
2012-10-08 13:42 1471Struts 2标签01 1.a 3 2.actio ... -
type="diapatcher Redirect RedirectAction"配置文件struts.xml
2012-10-08 11:23 3445type="diapatcher Redirec ...
相关推荐
struts实现的文件上传下载功能,最大可以上传到150MB
Struts2实现文件上传的功能,简洁方便,欢迎下载。
struts2 实现多文件上传功能,是一个很好的学习参考资料
ssh框架用struts2 hibernate实现图片的上传源码
自己做的一个struts2 实现文件批量上传功能,
struts1和struts2分别实现文件上传下载功能
JSP文件上传功能,用于实现Struts上传功能
struts2 用AJAX实现的上传功能,带进度条的jar包。
网上的Struts2进行的文件下载一般都是单文件或者固定的文件,并没有(很少)实现随意文件的下载的例子 提供多文件上传,上传成功后,提供刚上传的文件下载功能(其他的都可以在其上面进行扩充) 多文件 上传 下载...
一个利用Struts2实现文件的上传下载,使用的是同步方式。没有用ajax请求
基于Struts2的文件上传下载功能的完整源代码。经过测试可正确运行。
Struts2框架实现文件上传功能
在北大青鸟培训上课,老师用Strus2实现上传功能的代码
这是一个用struts2实现文件上传的例子,没有struts2的配置文件,可以上传到服务器上,
struts实现图片上传功能。包含详细的代码和配置信息。
用struts2实现用户的注册,涉及到的知识点包括用户名和密码的校验(正则表达式验证);国际化;radio,checkboxlist,optiontransferselect等标签的使用,文件的上传等。
NULL 博文链接:https://yqshfut.iteye.com/blog/619870
直接解压到tomcat的webapp目录下即可运行
完整的基于Struts2的上传下载功能实现,开发平台为MyEclipse
用struts2实现文件上传功能代码,这是技术培训老师讲的源代码。