博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一个jar包里的网站之文件上传
阅读量:7049 次
发布时间:2019-06-28

本文共 3157 字,大约阅读时间需要 10 分钟。

hot3.png

忙啊。码农就这命,抽不出时间写东西啊。

不过,在忙也得做知识积累啊。

工头说:有没有一种简单的配置能让tomcat变得很安全。

>_< 如果有,请大神悄悄的告诉我。

tomcat ,说起来似乎跟java 沾边的东西都会被人认为很复杂,相反的跟php沾边就会被认为很方便快捷。

好吧回到spring boot。spring boot demo里有文件上传的例子。

但是很多时候我门希望支持文件批量上传。额 ,这个demo就没有了,不过没关系自己动手丰衣足食。

首先我门需要一个页面,当然页面不是很漂亮,大家都是爷们就将就下吧,只要找的老婆漂亮,其他的好看与否都无所谓了。

<html>

<body>

<form method="POST" enctype="multipart/form-data"

action="/upload">

<input type="file" name="file" multiple="multiple">

<br />

<input type="submit"

value="Upload">

Press here to upload the file!

</form>

</body>

</html>

额,神马?你说multiple不支持ie6。现在,朕告诉你,凡是还使用ie6的愚民不是朕的子民,朕没有义务给他们发放福利。

额。。。难看是难看了点,不过使用bootstrap什么的孩子可以相当方面的美化下。

至于那些顾虑ie6的圣人贤者门,就自己写jquery 或者找插件吧。

spring boot 说要使用他的框架上传文件就必须要有一个MultipartConfigElement bean 

嗯,那就有吧,照抄就是了。

 @Bean    MultipartConfigElement multipartConfigElement() {        MultiPartConfigFactory factory = new MultiPartConfigFactory();        factory.setMaxFileSize("128KB");        factory.setMaxRequestSize("128KB");        return factory.createMultipartConfig();    }

128k什么的就不要吐槽了。

现在看原版的control  demo

@Controllerpublic class FileUploadController {    @RequestMapping(value="/upload", method=RequestMethod.GET)    public @ResponseBody String provideUploadInfo() {        return "You can upload a file by posting to this same URL.";    }    @RequestMapping(value="/upload", method=RequestMethod.POST)    public @ResponseBody String handleFileUpload(@RequestParam("name") String name,            @RequestParam("file") MultipartFile file){        if (!file.isEmpty()) {            try {                byte[] bytes = file.getBytes();                BufferedOutputStream stream =                        new BufferedOutputStream(new FileOutputStream(new File(name + "-uploaded")));                stream.write(bytes);                stream.close();                return "You successfully uploaded " + name + " into " + name + "-uploaded !";            } catch (Exception e) {                return "You failed to upload " + name + " => " + e.getMessage();            }        } else {            return "You failed to upload " + name + " because the file was empty.";        }    }}

看到这个代码我震惊了。多好的代码呀。嗯。。只需要把

MultipartFile file 变成list
 file 就是一活脱脱的批量上传代码。不过就是代码量有点大。。得改改。。

于是乎,本码农就加几行代码

public class FilesUpload {

private final static String FOLDER = "H:\\mytestfolder\\filesupload\\";

@RequestMapping(value = "/upload", method = RequestMethod.GET)

public @ResponseBody String provideUploadInfo() {

return "You can upload a file by posting to this same URL.";

}

@RequestMapping(value = "/upload", method = RequestMethod.POST)

public @ResponseBody String handleFileUpload(

@RequestParam("file") List<MultipartFile> files) {

List<String> message = new ArrayList<String>();

for (MultipartFile file : files) {

String name = file.getOriginalFilename();

if (!file.isEmpty()) {

try {

file.transferTo(new File(FOLDER + name));

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

message.add(name + " : ok ");

} else {

message.add(name + " :  null");

}

}

return message.toString();

}

}

.....完成了。。

亲们 欢迎复制,不过请注明出自 “这个世界不真实”。

参考地址:

转载于:https://my.oschina.net/lxy4java/blog/227342

你可能感兴趣的文章
filezilla链接服务器出现编码错误不能链接
查看>>
Markdown
查看>>
hdu2157 How many ways??(矩阵快速幂
查看>>
学习NLPIR语义智能教学科研平台要这样打开
查看>>
P1306 斐波那契公约数
查看>>
WPF/Silverlight 终极加速方案(无反射绑定)
查看>>
shell for mysql backup in linux
查看>>
js实现23种设计模式(收藏)
查看>>
ext container的使用的场景
查看>>
Slide + $.extend + $.fn.extend
查看>>
USACO全部月赛及GateWay数据
查看>>
7th-Activity基础
查看>>
B1008.数组元素循环右移问题(20)
查看>>
[BZOJ3601]一个人的数论
查看>>
软件工程网络15团队作业1
查看>>
CSS样式表引用方式
查看>>
以前写的一个C#读取UDP协议数据包程序
查看>>
iphone 如何破解xcode
查看>>
ZSL_List
查看>>
同或&&异或
查看>>