1

I have to upload an image from the user interface and change its name according to my need.

i.e. if user uploads an image with a name WIN_20151122_09_57_47_Pro.jpg I would have to change it to 1.jpg

I am using Spring mvc and for file upload and display I am using MultiPartFile datatype

Below is the model.

FileBucket.java

package com.faisal.model;

import org.springframework.web.multipart.MultipartFile ;

public class FileBucket {

      MultipartFile file ;

       public MultipartFile getFile() {
             return file ;
      }

       public void setFile(MultipartFile file ) {
             this.file = file;
      }
}

The controller part where I am uploading the file on the server

@Autowired
      ServletContext servletContext;

@RequestMapping(value = "/singleUpload" , method = RequestMethod.POST)
       public String singleFileUpload(@Valid FileBucket fileBucket,
                  BindingResult result, ModelMap model ) throws IOException {

            String PROFILE_UPLOAD_LOCATION = servletContext.getRealPath("/" )
                        + File. separator + "resources" + File.separator
                        + "profile_images" + File.separator;

             if (result .hasErrors()) {
                  System. out.println("validation errors" );
                   return "singleFileUploader" ;
            } else {
                  System. out.println("Fetching file" );
                  String destination=PROFILE_UPLOAD_LOCATION
                              + fileBucket.getFile().getOriginalFilename();

                  File file = new File(destination);
                  File newFile=new File(PROFILE_UPLOAD_LOCATION +"1.jpg" );


                  FileCopyUtils. copy(fileBucket.getFile().getBytes(), file);
                  FileUtils. moveFile(file, newFile);


                  MultipartFile multipartFile = fileBucket .getFile();
                  String fileName = multipartFile .getOriginalFilename();
                   model.addAttribute("fileName" , fileName );
                   return "success" ;
            }
      }

View page

<body>
       <div class="success" >

       <img src=" ${pageContext.request.contextPath}/resources/profile_images/${fileName} " height ="100" width="100" />
       <br/>
            File <strong> ${fileName}</strong > uploaded successfully.
       </div>

</body>

In the code above I have successfully renamed the file but then while redirecting on the same page the image is not displayed since it has been replaced by 1.jpg

Md Faisal
  • 2,931
  • 8
  • 29
  • 34
  • Try `` like this ``. Here's how I implemented [UserController](https://github.com/RawSanj/spring-tiles-sample-app/blob/master/src/main/java/com/sanjay/springtiles/controller/UserController.java) and in jsp view `User Image` – Sanjay Rawat Mar 25 '16 at 19:43
  • here is the thing in more simpler way: 1. uploading a file on server (which is accomplished) 2.uploading the file and renaming it at the server side to more appropriate name(This I am trying to accomplish now) – Md Faisal Mar 25 '16 at 20:54
  • @SanjayRawat this solved my problem , please write an answer so that I can accept it. – Md Faisal Mar 27 '16 at 06:43

1 Answers1

1

Try <spring:url> like this <img src='<spring:url value="/resources/profile-pictures/${user.profileImage}" />' >.

Here's how I implemented UserController and in jsp view <img src='<spring:url value="/resources/profile-pictures/${user.profileImage}" />' alt="User Image">

Sanjay Rawat
  • 2,304
  • 15
  • 30
  • I have encountered another problem where this same piece of code is working in one part of controller and not working in another here is the link to my question please take a look http://stackoverflow.com/questions/36250863/cannot-convert-value-of-type-java-lang-string-to-required-type-org-springfram – Md Faisal Mar 27 '16 at 18:20