如何使用jQuery文件上载和Spring引导解决java.io.FileNotFoundException
我正在尝试上载文件并将文件名保存到数据库。我已经添加了CommonMultipartResolver依赖项,并在Spring配置中定义了CommonMultipartResolver bean 错误 控制器如何使用jQuery文件上载和Spring引导解决java.io.FileNotFoundException,java,jquery,spring-boot,image-uploading,Java,Jquery,Spring Boot,Image Uploading,我正在尝试上载文件并将文件名保存到数据库。我已经添加了CommonMultipartResolver依赖项,并在Spring配置中定义了CommonMultipartResolver bean 错误 控制器 @PostMapping(value = "/uploadFile") @ResponseBody public ResponseEntity<Object> uploadFile(@RequestParam("uploadfile") MultipartFile
@PostMapping(value = "/uploadFile")
@ResponseBody
public ResponseEntity<Object> uploadFile(@RequestParam("uploadfile") MultipartFile uploadfile, Picture picture, Principal principal) {
User user = (User) ((UsernamePasswordAuthenticationToken) principal).getPrincipal();
picture.setUser(user);
try {
// Crop the image (uploadfile is an object of type MultipartFile)
BufferedImage croppedImage = cropImageSquare(uploadfile.getBytes());
// Get the filename and build the local file path
String filename = uploadfile.getOriginalFilename();
String directory = "/Users/molayodecker/Sites/admissionsPortal/data";
String filepath = Paths.get(directory, filename).toString();
String ext = FilenameUtils.getExtension(filename);
// Save the file locally
File outPutFile = new File(filepath);
ImageIO.write(croppedImage, ext, outPutFile);
//stream.write(uploadfile.getBytes());
} catch (Exception e) {
System.out.println(e.getMessage());
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
pictureService.save(picture, uploadfile);
return new ResponseEntity<>(HttpStatus.ACCEPTED);
} // method uploadFile
我认为您的Spring应用程序中的CSRF保护已启用。 您应该在ajax请求中添加_csrf_头、_csrf e、 g: 您还可以禁用csrf\u csrf\u头->
@Configuration
public class App extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
我认为您的Spring应用程序中的CSRF保护已启用。 您应该在ajax请求中添加_csrf_头、_csrf e、 g: 您还可以禁用csrf\u csrf\u头->
@Configuration
public class App extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
我得到一个错误
无法读取未定义的属性“toLowerCase”
我不知道它为什么工作,但现在我得到一个java.io.FileNotFoundException错误请添加完整堆栈跟踪我得到一个错误无法读取未定义的属性“toLowerCase”
我不知道它为什么工作,但现在我得到一个java.io.FileNotFoundException错误请添加完整堆栈跟踪
<form id="upload-file-input" th:action="@{/uploadFile}" method="post" th:object="${picture}"
enctype="multipart/form-data" class="form-inline inline new-item">
<input type="file" id="file" name="uploadfile" />
<span class="placeholder" data-placeholder="Choose an image...">Choose an image...</span>
<label for="file" class="button">Browse</label>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
var $formUploader = $("#upload-file-input");
$formUploader.on("submit", function(e){
e.preventDefault();
//var data = new FormData(this);
var data={};
$.each($(this).serializeArray(), function(i, field){
data[field.name] = field.value;
});
$.ajax({
//dataType: 'json',
url: $formUploader.prop('action'),
type: "POST",
//data: new FormData($("#upload-file-input")[0]),
data: data,
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
success: function (data) {
// Handle upload success
$("#upload-file-message").text("File succesfully uploaded");
},
error: function () {
// Handle upload error
$("#upload-file-message").text("File not uploaded (File might be big, size needed.)");
}
});
});
var header = $("meta[name='_csrf_header']").attr("content");
var token = $("meta[name='_csrf']").attr("content");
$.ajax({
url: '/test',
type: 'POST',
beforeSend: function(xhr){
xhr.setRequestHeader(header, token);
},
success: function(data) {
-----
}
}
@Configuration
public class App extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}