3

I am new to Spring MVC though not new to Java. I am trying to do upload and download data to and from a database. I did similar like in this website : file-upload-and-download-using-spring-mvc.

The upload file is successfully but when I wanna download file, the download is successfully but the file is corrupt. Why this is happen? and how to solve it?

This is my download controller..

@RequestMapping(value = "/listeContenuDownload.id", method = RequestMethod.GET)
    public ModelAndView responseEachReportDownload(ModelMap model, HttpServletRequest request, HttpSession session, @ModelAttribute("uploadForm") FileUploadForm uploadForm, Model map, HttpServletResponse response) throws IOException, ServletRequestBindingException, SQLException {

String tiket = request.getParameter("idTiket");
complaintdata cd = new GoIndex().getCheckStatusReport(tiket);
String nameFileReport = cd.getNameFileReport();
String extFileReport = cd.getExtFileReport();
byte[] file = cd.getFile();

response.setContentType(extFileReport);
response.setContentLength(file.length);
response.setHeader("Content-Disposition","attachment; filename=\"" + nameFileReport +"\"");

FileCopyUtils.copy(file, response.getOutputStream());

 return null;
   }

This is my download form in HTML-JSP :

<div class="form-group">
    <label class="col-md-3 control-label" for="message">File : </label>
    <div class="col-md-9">
        <a href="listeContenuDownload.id?idTiket=<c:out value="${ticket}"/>">${nameFileReport}</a>
    </div>
 </div>

This is upload controller :

@RequestMapping(value = "/getTicketting.id", method = RequestMethod.POST)
    public String userDataSubmit(ModelMap model, HttpServletRequest request, HttpSession session, @ModelAttribute("uploadForm") FileUploadForm uploadForm,
            Model map) throws SQLException, FileNotFoundException, FileUploadException, IOException, ServletException {

MultipartFile multipartFile = uploadForm.getFile();

            String fileName = "";
            String fileExt = "";
            byte[] file123 = null;

            if (multipartFile != null) {
                fileName = multipartFile.getOriginalFilename();    
                fileExt = multipartFile.getContentType();          
                file123 = multipartFile.getBytes();                 
            }

String urf = new GoIndex().getUpdateComplaintData(ticket, subject, data, file, fileName, fileExt, file123);

return "buatAduanTicket";
        }

This is my upload-form in HTML-JSP :

    <form class="form-horizontal" action="getTicketting.id" method="post" modelAttribute="uploadForm" enctype="multipart/form-data"> 
 <div class="clearfix">
    <label for="fileData"><span><b>Upload the File :</b></span></label>
    <div class="input">
        <input type="file" title="AddFile" name="file" id="file">
    </div>
  </div>
 </form>

What I mean with corrupt is for instance when I downloaded an image file, it will appear : "can't open this picture because the file appears to be damaged, corrupted or is too large" or a pdf file something like : "failed to load pdf "document". ps: the download is successfully but can't open the download file.

Many thanks in advance for any help.

2
  • Define "corrupt". Can you post the HTTP headers received with the download on the client side? Commented Jan 24, 2015 at 11:25
  • I added the dowload controller. The download is successfully but can't open the download file, will appear something like "can't open this picture because the file appears to be damaged, corrupted or is too large" (image file) or "failed to load pdf "document" (pdf file) Commented Jan 25, 2015 at 14:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.