如何使用spring和RESTAPI在数据库中上传图像?

xtupzzrd  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(234)

我已经阅读了很多关于如何使用spring上传图像的教程,但是它们都是通过表单完成的,如果我想使用rest上传图像呢?
这是我的html页面:

<body>
    <div id="add-movie-form-div">    
        <label>Name</label> <input type="text" id="name-input"> 
        <label>Description</label> <input type="text"  id="decription-input"> 
        <label>Length</label> <input type="text" id="length-input"> 
        <label>Release date</label> <input type="text" id="release-date-input"> 
        <label>Upload picture</label>  <input type="file" id="image-input">                       
        <button id="add-movie-btn">Add</button>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="/jquery/insert-movie.js"></script>
</body>

这是jquery:

let insertButton = $("#add-movie-btn");

    insertButton.on('click', function () {
    let selectTag = $("#item-selected option:selected").text();
    let nameInput = $("#name-input").val();
    let descriptionInput = $("#decription-input").val();
    let lengthInput = $("#length-input").val();
    let releaseDateInput = $("#release-date-input").val();

let movie = {
    "name": nameInput,
    "length": lengthInput,
    "releaseDate": releaseDateInput,
    "description": descriptionInput
};

$xhr = $.ajax({
    method: "POST",
    url: "http://localhost:8080/create",
    dataType: "json",
    contentType: "application/json",
    data: JSON.stringify(movie),
    success: success,
    error: error
 });
});

我有一个简单的restcontroller:

@RestController
public class MovieRestController {

@Autowired
MovieRepository movieRepo;

@PostMapping(value = "/create", consumes = "application/json")
public Movie createMovie(@RequestBody Movie movie) {
    return movieRepo.save(movie);
}

这样的代码工作得很好,但是如果我还想在mysql数据库中上传图片,我应该如何继续呢?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题