I have trying to work with spring boot, I want to make an app that uploads a file and returns the hash of it. I have successfully uploaded the file but I don`t know how to return the hashed value of it to a new html (or the same html page). Could you please help? Here is my code :
@Controller
public class FileUploadController {
@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) throws NoSuchAlgorithmException, IOException {
byte[] fileBytes = file.getBytes();
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] digest = sha256.digest(fileBytes);
String hashString = new BigInteger(1, digest).toString(16);
System.out.println("File hash: " + hashString);
return "redirect:/test.html";
}
And the html :
<form method="POST" enctype="multipart/form-data" action="/">
<table>
<tr>
<td>File to upload:</td>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Upload" /></td>
</tr>
</table>
</form>