ファイルリストがあるページがあります。 .txtのいずれかをクリックするとダウンロードされ、GoogleChromeのダウンロードの通知として通知が表示されます。春にダウンロードされないファイル
これは.txtファイルをクリックした後に呼び出されるJsです ここでは、私はファイル名と選択したファイルのファイルパスを取得しています。そして、それらのファイル名とファイルパスをSpringサーブレットに送信するajaxを使用します。
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();
}
}
}
何か提案がありますか?
を見ることができます。ここ
と同じくらい簡単です。ストリームをコピーした後 – VincentブラウザにファイルURLを入力すると、そのファイルがダウンロードされますか? – reos
@reosブラウザにURLを直接入力すると、ファイルが開かれます。 –