程序猿全敏
2016-02-22 10:49:44
原
struts2开发文件上传下载的简单实例
这次帮同学搞毕业设计,遇到一个上传下载的问题,于是我就在网上到处找资料,通过理解,终于搞定了,最后遇到一个乱码的问题,谢谢@广州-吢涼ㄋ 大神帮我解决了,不多说上代码,希望你们用的到。
提交的核心代码
@Override public String execute() throws Exception { String root=ServletActionContext.getServletContext().getRealPath("/file"); InputStream is=new FileInputStream(file); OutputStream os = new FileOutputStream(new File(root, fileFileName)); System.out.println("fileFileName"+fileFileName); // 因为file是存放在临时文件夹的文件,我们可以将其文件名和文件路径打印出来,看和之前的fileFileName是否相同 System.out.println("file: " + file.getName()); System.out.println("file: " + file.getPath()); byte[] buffer=new byte[500]; int length=0; while(-1 !=(length=is.read(buffer,0,buffer.length))){ os.write(buffer); } os.close(); is.close(); return SUCCESS; }
下载的核心代码比较少
return ServletActionContext.getServletContext().getResourceAsStream("file/朱艺璇。.txt");
然后就是运行截图
上传的:开始前
下载截图:
下载后就ok啦!
猜你喜欢
请下载代码后再发表评论
文件名:uplode.rar,文件大小:8.406K
下载
- /
- /uplode
- /uplode/.classpath
- /uplode/.mymetadata
- /uplode/.mystrutsdata
- /uplode/.project
- /uplode/.settings
- /uplode/.settings/org.eclipse.core.resources.prefs
- /uplode/.settings/org.eclipse.jdt.core.prefs
- /uplode/WebRoot
- /uplode/WebRoot/index.jsp
- /uplode/src
- /uplode/src/com
- /uplode/src/struts.xml
- /uplode
![最代码相关代码源代码列表](https://static.zuidaima.com/resource/img/heart_ico.png)
![最代码最近下载分享源代码列表](https://static.zuidaima.com/resource/img/heart_ico.png)
酒非儿 LV6
2021年12月3日
bcr1234 LV9
2020年8月6日
cz206616 LV10
2020年5月29日
王可以 LV10
2020年4月22日
oldtrybest LV8
2019年8月29日
11ccvvbb LV6
2019年4月10日
wdlsuper LV4
2019年1月24日
zzzxxx32 LV3
2019年1月8日
斤斤计较jh LV1
2018年12月9日
chengqiang LV13
2018年6月29日
![最代码最近浏览分享源代码列表](https://static.zuidaima.com/resource/img/cattle_ico.png)
lironggang LV38
2022年11月20日
uni-code_0123 LV1
2022年11月16日
雷迪斯俺的乡亲们 LV11
2022年3月9日
zhangmingzuidaima LV3
2021年12月16日
酒非儿 LV6
2021年12月3日
dongzhan LV12
2021年11月27日
sl0018 LV13
2021年10月3日
ALT_LB LV4
2021年8月23日
isMrQt LV1
2021年7月15日
MrQian LV1
2021年6月30日