您的位置:首页 > 运维架构 > 网站架构

一个jar包里的网站之文件上传

2014-04-27 22:10 1636 查看

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

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

工头说:有没有一种简单的配置能让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<MultipartFile> file 就是一活脱脱的批量上传代码。
不过就是代码量有点大。。得改改。。

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

@Controller

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();

}

}

.....完成了。。

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

参考地址:http://spring.io/guides/gs/uploading-files/





















































内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息