Jason
Jason

Reputation: 11363

Transfer file to Tomcat location

I'm trying to implement file upload functionality in a Spring application.

Currently, I am using the multiple attribute of HTML5 forms to send multiple files to the server. The files are htting the controller, but I am having issues transferring them to the server destination.

@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String handleUpload(@RequestParam("files[]") List<MultipartFile> files, Model model) {

    String fileName;
    File transferFile;
    String filePath = System.getProperty("catalina.base") + File.separator + "resources" + File.separator;
    ArrayList<File> fileList = new ArrayList<File>(files.size());
    ArrayList<String> fileNameList = new ArrayList<String>(files.size());


    for (MultipartFile file : files) {

        fileName = filePath + file.getOriginalFilename();
        transferFile = new File(fileName);
        fileNameList.add(fileName);

        try {
            if (transferFile.exists()) {
                logger.info("Successful Transfer!");
                file.transferTo(transferFile);
            else 
                logger.info("Could not create file at " + fileName);

I left out the catch blocks and other logging but the transferFile object is created but it does not exist at the location.

How can I create the file at the specified location?

Upvotes: 0

Views: 395

Answers (1)

Ruju
Ruju

Reputation: 971

  1. check you added enctype='multipart/form-data' in form tag.

Check you define in configuration file spring.xml

 <beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <!-- one of the properties available; the maximum file size in bytes -->
          <beans:property name="maxUploadSize" value="100000000" />
 </beans:bean>

2.check the name atribute of your file tag and use same in your controller method handleUpload.

3.Check whether the file is created or not at specified location by you in which you are transfering file from user i.e. check transferFile creates file or not at destination.

Upvotes: 2

Related Questions