我正在制作一个Spring Boot应用程序来提供图像。我的createFile
方法尝试从POST请求获取URI,但无法解析文件名。
下面,我试图发出POST请求以提供图像。我在URI下的resolve
方法上有错误。我尝试了3种进口,如下所述:
//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;
这些似乎都不起作用。
以下是我要提供的POST请求,以提供图像。
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());
}
}
答案 0 :(得分:2)
嗨,请按如下所示将Uri更改为URI
final URI locationUri = new URI(servletRequest.getRequestURL().toString() + "/")
.resolve(file.getOriginalFilename() + "/raw");