本文目錄一覽:
- 1、jsp實現文件的下載
- 2、jsp頁面如何實現下載文檔
- 3、JSP通過超鏈接下載文件
- 4、請問誰會文件的上傳和下載啊,基於jsp的,直接右擊連接,另存為的那種
- 5、Java的jsp頁面提交不跳轉,怎麼自動成下載功能了! 奇怪了?! 請幫我看下,謝謝了。 Jav
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通過超鏈接下載文件
JSP頁面點擊超鏈接彈出文件下載,代碼如下:
%
String path = request.getContextPath();
String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%
//然後
a href =”%= basePath %/upload/aa.doc }” target=”_blank”下nbsp;nbsp;載/a
註:%= basePath %獲取部署JSP項目的根目錄,/upload/aa.doc/是根目錄uploadaa.doc文件,根據需求修改即可。
請問誰會文件的上傳和下載啊,基於jsp的,直接右擊連接,另存為的那種
要用到jspSmartUpload組件,先到網站下載這個組件(或直接搜它),下載解壓後,把Web-inf/classes下的文件打成JAR包,放到Tomcat的,lib下,再在你的項目中導入此JAR包。
下面附代碼給你,去試試:
jspSmartUpload.html
head
/head
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″
body
CENTER
FONT SIZE = 5 COLOR = blue一個關於文件上傳的例子/FONT
/CENTER
BR
HR
BR
form method=”post” action=”jspSmartUpload.jsp” enctype=”multipart/form-data”
input type=”hidden” name=”TEST” value=”good”
table width=”80%” border=”0″ align=”center”
tr
td1.
input type=”FILE” name=”FILE1″ size=”30″
/td
/tr
tr
td2.
input type=”FILE” name=”FILE2″ size=”30″
/td
/tr
tr
td
center
brinput type=”submit” name=”Submit” value=”上傳”
/center
/td
/tr
/table
/form
/body
/html
jspSmartUpload.jsp
html
head
title文件上傳成功/title
/head
%@ page contentType=”text/html; charset=gb2312″%
%@ page import=”java.io.File,com.jspsmart.upload.*”%
body
CENTER
FONT SIZE = 5 COLOR = blue恭喜您!文件上傳成功/FONT
/CENTER
BR
HR
BR
%
//新建一個SmartUpload對象
com.jspsmart.upload.SmartUpload su = new SmartUpload();
// 上傳初始化
su.initialize(pageContext);
//上傳文件
su.upload();
// 將上傳文件全部保存到指定目錄
int count = su.save(“/upload/”);
out.println(“成功上傳”+count+”個文件!br”);
%
p
上傳文件的信息如下:
/p
table border=1 align=”center”
tr
td文件編號/td
td文件大小(字節)/td
td文件名/td
td文件類型/td
/tr
%
//逐一提取上傳文件信息,同時可保存文件。
for(int i=0;isu.getFiles().getCount();i++)
{
com.jspsmart.upload.File file = su.getFiles().getFile(i);
//若文件不存在則繼續
if(file.isMissing()) continue;
//顯示當前文件信息
%
tr
td%=file.getFieldName()%/td
td%=file.getSize()%/td
td%=file.getFileName()%/td
td%=file.getFileExt()%/td
/tr
%
}%
/table
/body
/html
downloadFile.html
html
head
title下載文件/title
/head
meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″
body
CENTER
FONT SIZE = 5 COLOR = blue下載文件/FONT
/CENTER
BR
HR
BR
p要下載的文件是:/p
p
test.doc
/p
center
a href=”downloadFile.jsp”單擊下載/a
/center
/body
/html
downloadFile.jsp
html
head
title文件下載處理頁面/title
/head
%@ page contentType=”text/html; charset=gb2312″%
%@ page import=”com.jspsmart.upload.*”%
body
%
// 新建一個SmartUpload對象
SmartUpload su=new SmartUpload();
// 初始化
su.initialize(pageContext);
// 設定contentDisposition為null以禁止瀏覽器自動打開文件
su.setContentDisposition(null);
// 下載文件
su.downloadFile(“/download/test.doc”);
%
/body
/html
Java的jsp頁面提交不跳轉,怎麼自動成下載功能了! 奇怪了?! 請幫我看下,謝謝了。 Jav
看一下addbook2.jsp頭部代碼,你檢查下頭部,估計是頭部寫錯了
要不你直接複製下面的,替換一下
%@ page language=”java” contentType=”text/html; charset=UTF-8″
pageEncoding=”UTF-8″%
原創文章,作者:GUGRX,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/325002.html