欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot:upload

程序员文章站 2022-04-30 12:55:53
...
FileUploadConfiguration.java
===================================================
@Configuration
public class FileUploadConfiguration {

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置文件大小限制 ,超出设置页面会抛出异常信息,
        // 这样在文件上传的地方就需要进行异常信息的处理了;
        factory.setMaxFileSize("256KB"); // KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize("512KB");
        // Sets the directory location where files will be stored.
        // factory.setLocation("路径地址");
        return factory.createMultipartConfig();
    }

}
===================================================
Controller.java
===================================================
    /**
     * 文件上传具体实现方法(单文件上传)
     *
     * @param file
     * @return <form method="POST" enctype="multipart/form-data" action="/upload">
     * <p>
     * 文件:<input type="file" name="file" />
     * </p>
     * <p>
     * <input type="submit" value="上传" />
     * </p>
     * </form>
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                // 这里只是简单例子,文件直接输出到项目路径下。
                // 实际项目中,文件需要输出到指定位置,需要在增加代码处理。
                // 还有关于文件格式限制、文件大小限制,详见:中配置。
                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(file.getOriginalFilename())));
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
                return "上传失败," + e.getMessage();
            }
            return "上传成功";
        } else {
            return "上传失败,因为文件是空的.";
        }
    }

    /**
     * 多文件上传 主要是使用了MultipartHttpServletRequest和MultipartFile
     *
     * @param request
     * @return <form method="POST" enctype="multipart/form-data"
     * action="/upload/batch">
     * <p>
     * 文件1:<input type="file" name="file" />
     * </p>
     * <p>
     * 文件2:<input type="file" name="file" />
     * </p>
     * <p>
     * 文件3:<input type="file" name="file" />
     * </p>
     * <p>
     * <input type="submit" value="上传" />
     * </p>
     * </form>
     */
    @RequestMapping(value = "/upload/batch", method = RequestMethod.POST)
    public
    @ResponseBody
    String batchUpload(HttpServletRequest request) {
        List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
        MultipartFile file = null;
        BufferedOutputStream stream = null;
        for (int i = 0; i < files.size(); ++i) {
            file = files.get(i);
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
                    stream.write(bytes);
                    stream.close();
                } catch (Exception e) {
                    stream = null;
                    return "You failed to upload " + i + " => " + e.getMessage();
                }
            } else {
                return "You failed to upload " + i + " because the file was empty.";
            }
        }
        return "upload successful";
    }
相关标签: springboot upload