I have a Controller
:
@Controller
public class ImageController {
@GetMapping("/upload")
public String uploadImageGet(Model model) {
return "uploadForm";
}
@PostMapping("/upload")
public String uploadImagePost(@ModelAttribute Image image, Model model) throws IOException {
// what should I do here?
return "result";
}
}
An HTML form:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="#" th:action="@{/upload}" th:object="${image}" method="post" enctype="multipart/form-data">
<label for="name">Name:</label><br>
<input type="text" id="name" name="name" value=""><br>
<label for="description">Description:</label><br>
<input type="text" id="description" name="description" value=""><br>
<label for="author">Author:</label><br>
<input type="text" id="author" name="author" value=""><br>
<label for="image">Image:</label><br>
<input type="file" id="image" name="image"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
And a class for storing the form data:
public class Image {
private String name;
private String author;
private String description;
private MultipartFile image;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public MultipartFile getImage() {
return image;
}
public void setImage(MultipartFile image) {
this.image = image;
}
}
I want to send the form data to another host (Spring API), but also display a "Uploaded successfully" response page. Because of that I wanted to handle all that in the controller, but can't figure out how to do this. I was able to find some resources on creating a request manually, but this seems like there must be some simpler way to do this. If I'm approaching it wrong please let me know.
question from:
https://stackoverflow.com/questions/65918598/how-to-forward-form-data-to-another-host-in-spring-mvc-controller 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…