2

I am making a Spring Boot Application to serve images. My createFile method tries to get URI from the POST request but it is unable to resolve the files' name.

Down below I am trying to make a POST request for serving images. I have the the error on resolve method under URI. I tried 3 imports as stated below:

//1
import java.net.URI;
//2
import com.sun.org.apache.xml.internal.utils.URI;
//3
import com.sun.org.apache.xerces.internal.util.URI;

None of these seems to work.

Below is the POST request I am trying to make to serve images.

private static final String BASE_PATH = "/images";
//for Curl. Development and testing
@RequestMapping(method = RequestMethod.POST,value = BASE_PATH )
@ResponseBody
public ResponseEntity<?> createFile(@RequestParam("file") MultipartFile file, HttpServletRequest servletRequest) {

    try {
        imageService.createImage(file);
        final URI locationUri = new Uri(servletRequest.getRequestURL().toString() + "/")
                .resolve(file.getOriginalFilename() + "/raw");//error here on resolve
        return ResponseEntity.created(locationUri)
                .body("Succesfull" + file.getOriginalFilename());


    } catch (IOException e) {
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body("Could not find " + file.getOriginalFilename() + "=>" + e.getMessage());

    }
}
Alex Smith
  • 463
  • 2
  • 8
  • 17

1 Answers1

2

Hi Please change Uri to URI like below

final URI locationUri = new URI(servletRequest.getRequestURL().toString() + "/")
                            .resolve(file.getOriginalFilename() + "/raw");
Ganesh Gudghe
  • 1,327
  • 1
  • 17
  • 41