0

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>
Michael Petch
  • 46,082
  • 8
  • 107
  • 198

2 Answers2

0

You already have it in your code. Use RedirectAttributes

 public String handleFileUpload(......,RedirectAttributes redirectAttributes)..
    {
     <your logic goes here>
     redirectAttributes.addAttribute("hashString", hashString);
     return "redirect:/test.html";
    }

and later get that attribute from request parameter of the new page.

Arun
  • 3,701
  • 5
  • 32
  • 43
  • Hi, thank you for the answer, it works! but I would like to return it to the html itself...How can I do that...? Thank you so much! – Steve Goldshmit Aug 30 '17 at 18:19
  • Try jQuery ajax for asynchronous upload and return the data after upload and display the message in a div. This link may help you. https://stackoverflow.com/questions/4856917/jquery-upload-progress-and-ajax-file-upload/4943774#4943774 – Arun Aug 31 '17 at 01:46
  • Hi Arun, It is a bit complicated for me..I am new to this..is there a way to show values on html documents in a simpler way? – Steve Goldshmit Aug 31 '17 at 11:39
0

I have done research and found out that one of the ways to return a value is with the thymeleaf resource, so my code goes like this now and it works! package com.boot.test10.controller;

import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class ViewController {

    private String sha256String;

    @RequestMapping("/")
    public String Test(){
        return "index";
    }

    @RequestMapping("/hash")
    public String index(Model model){
        model.addAttribute("hash", sha256String);
        return "hash";
    }


    @PostMapping("/")
    public String handleFileUpload(@RequestParam("file") MultipartFile file,
            RedirectAttributes redirectAttributes) throws IOException, 
    `enter code here`NoSuchAlgorithmException {

        byte[] fileBytes = file.getBytes();
        MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
        byte[] digest = sha256.digest(fileBytes);
        String sha256String = new BigInteger(1, digest).toString(16);
        this.sha256String = sha256String;

        return "redirect:/hash";
    }

    }

and the html is this :

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>Test Me</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous" />
</head>
<body>
<p th:text="'The Hash Of The File Is: ' + ${hash}"></p>
</body>
</html>