File Upload and Download using Spring (mvc)

南楼画角 提交于 2019-12-14 04:16:48

问题


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.

来源:https://stackoverflow.com/questions/28124786/file-upload-and-download-using-spring-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!