我是Spring MVC的新手,虽然不是Java的新手。我正在尝试上传和下载数据库中的数据。 我在网站上做了类似的事情:file-upload-and-download-using-spring-mvc。
上传文件成功但是当我想下载文件时,下载成功但文件已损坏。为什么会这样?以及如何解决它?
这是我的下载控制器..
@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;
}
这是我在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>
这是上传控制器:
@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";
}
这是我在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>
我对腐败的意思是,例如当我下载图像文件时,它会显示:&#34;无法打开此图片,因为该文件似乎已损坏,损坏或太大而且#34;或者pdf文件类似于:&#34;无法加载pdf&#34;文档&#34;。 ps:下载成功但无法打开下载文件。
非常感谢您的帮助。