本文目錄一覽:
- 1、jsp中mp3文件下載要怎麼實現?
- 2、jsp 下載文件路徑問題
- 3、jsp實現文件的下載
- 4、jsp頁面如何實現下載文檔
- 5、jsp 如何實現文件上傳和下載功能?
- 6、jsp如何實現文件上傳與下載?
jsp中mp3文件下載要怎麼實現?
有很多種方式:
1、直接request.sendredirect(“music/test.mp3”);
2、如果你想隱藏路徑:
RequestDispatcher rd = sc.getRequestDispatcher(“/music/test.mp3”).forward(request, response);
3、或者直接用流的方式返回給用戶。
如果返回mp3文件瀏覽器直接打開亂碼文件,你需要在web.xml添加mime映射:
mime-mapping
extensionmp3/extension
mime-typeaudio/x-mpeg/mime-type
/mime-mapping
jsp 下載文件路徑問題
下載文件有兩種方式。
1.是在你的伺服器上能相對找到。
即 這個映射的是你伺服器上的 D:\web 這個目錄
那麼你這個文件就要在 D:\web 這個目錄中。
比如 D:\web\downfile\111.xls
你的超鏈接可以這樣寫。 a href=”/downfile/111.xls”download/a
2.就是用流的方式下載。
a href=”#” onclilck=”……”download/a
這樣的超鏈接就不是指向一個文件了,而是向伺服器提交下載申請。
這樣執行到你後台的servlet類中,你可以根據一些必要的標識知道你要下載的文件。
這樣你把D:\111.xls文件讀取出來。 然後寫入到response.getOutPutStream (這個方法有些記不清了,你查一下) 這樣實現下載。
jsp實現文件的下載
%@pagelanguage=”java” import=”java.io.*,java.net.*” contentType=”application/x-msdownload” pageEncoding=”UTF-8″%%
//關於文件下載時採用文件流輸出的方式處理:
//加上response.reset(),並且所有的%後面不要換行,包括最後一個;
String url = request.getParameter(“url”);
System.out.print(url);
int k = url.lastIndexOf(“\\”);
String url1=url.substring(k+1,url.length());
response.reset();//可以加也可以不加
response.setContentType(“application/x-download”);
String filedownload = url;
String filedisplay = url1;
filedisplay = URLEncoder.encode(filedisplay,”UTF-8″);
response.addHeader(“Content-Disposition”,”attachment;filename=” + filedisplay);
OutputStream outp = null;
FileInputStream in = null;
try
{
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while((i = in.read(b)) 0)
{
outp.write(b, 0, i);
}
out.clear();
out = pageContext.pushBody();
outp.flush();
}
catch(Exception e)
{
System.out.println(“Error!”);
}
finally
{
if(in != null)
{
in.close();
in = null;
}
if(outp != null)
{
outp.close();
outp = null;
}
}
%
jsp頁面如何實現下載文檔
jsp頁面下載文檔是在jsp中有一個a標籤 ,當用戶點擊a標籤的時候下載文件。
一般採用href屬性直接指向一個伺服器地址,只要鏈接的文件存在,就會給出彈出保存對話框.
點擊a標籤 先執行onclick事件,再請求href中指向的地址。
前端jsp:
a href=”#” onclick=”javascript:downloadtest(‘${app.id}’)” id=”pluginurl” style=”color: #83AFE2;text-decoration:underline;”/a
然後在js中:
function downloadtest(id){
var url = “%=request.getContextPath()%/app/download” + “/” + id;
$(“#pluginurl”).attr(“href”,url);
}
後台處理下載邏輯的java代碼:
/**
* 下載文件
* @param id appid
* @param response
*/
@RequestMapping(value=”/download/{id}”)
public void download(@PathVariable String id, HttpServletResponse response){
String filepath = “”;
Result result = appService.getAppById(id);
App app = (App) result.getMap().get(“app”);
if(app == null){
return;
}
filepath = app.getUrl();
File file = new File(filepath);
InputStream inputStream = null;
OutputStream outputStream = null;
byte[] b= new byte[1024];
int len = 0;
try {
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
response.setContentType(“application/force-download”);
String filename = file.getName();
filename = filename.substring(36, filename.length());
response.addHeader(“Content-Disposition”,”attachment; filename=” + URLEncoder.encode(filename, “UTF-8”));
response.setContentLength( (int) file.length( ) );
while((len = inputStream.read(b)) != -1){
outputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
jsp 如何實現文件上傳和下載功能?
上傳:
MyjspForm mf = (MyjspForm) form;// TODO Auto-generated method stub
FormFile fname=mf.getFname();
byte [] fn = fname.getFileData();
OutputStream out = new FileOutputStream(“D:\\”+fname.getFileName());
Date date = new Date();
String title = fname.getFileName();
String url = “d:\\”+fname.getFileName();
Upload ul = new Upload();
ul.setDate(date);
ul.setTitle(title);
ul.setUrl(url);
UploadDAO uld = new UploadDAO();
uld.save(ul);
out.write(fn);
out.close();
下載:
DownloadForm downloadForm = (DownloadForm)form;
String fname = request.getParameter(“furl”);
FileInputStream fi = new FileInputStream(fname);
byte[] bt = new byte[fi.available()];
fi.read(bt);
//設置文件是下載還是打開以及打開的方式msdownload表示下載;設置字湖集,//主要是解決文件中的中文信息
response.setContentType(“application/msdownload;charset=gbk”);
//文件下載後的默認保存名及打開方式
String contentDisposition = “attachment; filename=” + “java.txt”;
response.setHeader(“Content-Disposition”,contentDisposition);
//設置下載長度
response.setContentLength(bt.length);
ServletOutputStream sos = response.getOutputStream();
sos.write(bt);
return null;
jsp如何實現文件上傳與下載?
如果伺服器端程序使用的是struts2框架的話,我會,其他的不會。
struts2:
對於上傳,jsp頁面只需要有個file類型的表單域,如input type=”file” name=”xxx” /
struts2的接收請求的action中再寫三個屬性與這個表單域的名稱對應起來,他們是,File類型的xxx,String類型的xxxFileName,String類型的xxxContentType,並其設置相應的set/get方法。則框架負責接收上傳文件的位元組流,解析文件名,文件類型,直接使用即可。
對於下載,只需要在action的配置文件中設置如下返回值類型和相應參數:
result type=”stream”
param name=”contentType”application/octet-stream/param
param name=”inputName”inputStream/param
param name=”contentDisposition”attachment;filename=xxx /param xxx為下載文件的文件名
/result
且在action總寫一個返回值類型為InputStream的getInputStream方法,此方法返回你要下載的文件的流即可。
ps:其中contentDisposition的配置信息中attachment代表點擊下載時瀏覽器先彈出個保存位置的提示框,然後再決定是否下載,默認是inline,即直接打開文件。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/243677.html