在src下新建,struts.properties文件,文件名是固定,用来更改一些默认配置。
可以在struts2的jar包下找到默认的配置文件,一般不不去修改修改那个文件。
复制代码
1
2struts.multipart.saveDir=d:/upload struts.multipart.maxSize=9000000
struts2上传文件并不是我们想象的直接上传那么简单。
需要先上传到临时文件中,最后在移动到指定的目录中。
批量上传是这样的,现在表单中把name设置为一样的。
复制代码
1
2
3
4
5
6<form action="upload.action" method="post" enctype="multipart/form-data"> <input type="file" name="files"><br> <input type="file" name="files"><br> <input type="file" name="files"><br> <input type="submit" value="上传"> </form>
然后在action中这样处理
复制代码
1
2
3private List<File> files; //命名要一致!! private List<String> filesFileName; //后两个命名格式是固定的 private List<String> filesContentType;
剩下的就是遍历了,各个击破
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18public String execute() throws Exception { //放在了files文件下 String path = ServletActionContext.getRequest().getRealPath("/files"); int n = files.size(); for(int i=0; i<n; i++){ InputStream is = new FileInputStream(files.get(i)); OutputStream os = new FileOutputStream(new File(path,filesFileName.get(i))); byte[] buffer = new byte[1024*10]; int length = 0; while( -1 != (length = is.read(buffer))){ os.write(buffer, 0, length); } is.close(); os.close(); } return SUCCESS; } }
最后
以上就是舒适汽车最近收集整理的关于struts2文件上传,设置临时目录和限制文件大小 以及 批量上传的全部内容,更多相关struts2文件上传,设置临时目录和限制文件大小内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复