Pergunta

I try to upload some files using Spring Boot. And there is a question, where should i point path where my files will be store. You can see my UploadController, template and Application class bellow.

@Controller
public class FileUploadController {

    @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.";
        }
    }

}

Here is my form.

<div class="container">
            <form method="POST" enctype="multipart/form-data"
                  action="/upload">
                File to upload:
                <input type="file" name="file"/><br/> Name:
                <input
                    type="text" name="name"/><br/> <br/> <input type="submit"
                                                               value="Upload"/> Press here to upload the file!
            </form>
        </div>

Here is Application class.

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

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

}
Foi útil?

Solução

This following line of code contains where is your file added.

new File(name + "-uploaded")

Since you didn't specify a complete path it will be saved on the java classpath were you started the application from.

For production systems you should specify it at a certain shared storage where files are got backuped regularly and the size of the storage are suitable of the kind of files you are uploading.

If you are using windows here is an example for you:

new File("d:/yourApplicationName/" + name + "-uploaded")

If you are using linux here is an example for you:

new File("/home/yourApplicationName/" + name + "-uploaded")

Note: You should create the directory "yourApplicationName" first or you will get an exception.

Outras dicas

I don't think there is a "right" answer to this question. Put the file wherever you like, or nowhere if it suits you. It all depends on what the actual business purpose is.

Sure you need register resolver for multipart requests. Also you need add dependencies to apache common uploads for this sample works. Problem related with change in RC4 of Sping Boot

It's shold seems like

@Configuration
public class Application{
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        return new CommonsMultipartResolver();
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top