파일 목록이있는 페이지가 있습니다. 임의의 .txt에 올라 타면 다운로드 받아야하며 알림은 GoogleChrome에서 다운로드 한 알림으로 표시되어야합니다.봄에 다운로드되지 않는 파일
이것은 .txt 파일을 cliking 한 후 호출되는 Js입니다. 여기 내가 선택한 파일의 파일 이름과 파일 경로가 나타납니다. 그리고 나서 아약스를 사용하여 파일 이름과 파일 경로를 스프링 서블릿에 보냅니다.
if (options.downloadable) {
$(easyTree).find('.easy-tree-toolbar').append('<div class="fileDownload"><button class="btn btn-default btn-sm btn-primary"><span class="glyphicon glyphicon-circle-arrow-down"></span></button></div>');
$(easyTree).find('.easy-tree-toolbar .fileDownload > button').attr('title', options.i18n.downloadTip).click(function() {
var selected = getSelectedItems();
var fileName = $(selected).find(' > span > a').text();
alert("fileName**" + fileName);
var hrefValue = $(selected).find(' > span > a').attr('href');
alert("hrefValue**" + hrefValue);
if (selected.length <= 0) {
$(easyTree).prepend(warningAlert);
$(easyTree).find('.alert .alert-content').html(options.i18n.downloadNull);
} else {
$.ajax({
url: "/ComplianceApplication/downloadFileFromDirectory",
type: "GET",
data: {
hrefValue: hrefValue,
fileName: fileName
},
success: function(data) {
//$(selected).remove();
//window.location.reload();
},
error: function(e) {
}
});
}
});
}
이것은 나의 springController입니다. 여기에 모든 데이터가 제대로 전달되지만 문제는 파일이 다운로드되지 않고 오류가 발생하지 않아서 내가 어떤 실수를하는지 알 수 있습니다.
@RequestMapping(value="/downloadFileFromDirectory",method = RequestMethod.GET)
public @ResponseBody void downloadFileFromDirectory(@PathVariable("fileName") String fileName,@RequestParam(value = "hrefValue") String hrefValue,HttpServletRequest request, HttpServletResponse response,Model model){
System.out.println("hrefValue***"+hrefValue);
String filePath = hrefValue;
ServletContext context = request.getSession().getServletContext();
File downloadFile = new File(filePath);
FileInputStream inputStream = null;
OutputStream outStream = null;
try {
inputStream = new FileInputStream(downloadFile);
response.setContentLength((int) downloadFile.length());
response.setContentType(context.getMimeType(downloadFile.getName()));
// response header
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
//String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
response.setHeader(headerKey, headerValue);
// Write response
outStream = response.getOutputStream();
IOUtils.copy(inputStream, outStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != inputStream)
inputStream.close();
if (null != outStream)
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
제안 사항 ???
public ResponseEntity<InputStreamResource> downloadFile(@PathVariable("idForm") String idForm)
{
try
{
File parent = new File(csvFilesPath);
File file = new File(parent, idForm+".csv");
HttpHeaders respHeaders = new HttpHeaders();
MediaType mediaType = new MediaType("text","csv");
respHeaders.setContentType(mediaType);
respHeaders.setContentLength(file.length());
respHeaders.setContentDispositionFormData("attachment", "file.csv");
InputStreamResource isr = new InputStreamResource(new FileInputStream(file));
return new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK);
}
catch (Exception e)
{
String message = "Errore nel download del file "+idForm+".csv; "+e.getMessage();
logger.error(message, e);
return new ResponseEntity<InputStreamResource>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
나는 일반적으로 response.flushBuffer()를 사용을 볼 수 있습니다 간단 여기
로이다; 스트림을 복사 한 후 – Vincent브라우저에 파일 URL을 넣으면 파일이 다운로드됩니까? – reos
@reos 브라우저에 URL을 입력하면 파일이 열립니다. –