jsp页面如何实现远程文件下载,而且下载时要出现一个提示框提醒是保存还是下载
是在一个局域网内的文件下载,只要知道对方资源的url就可以下载,url类似于:http://192.168.0.12:8080/downfiles/xx.doc,只要传过...
是在一个局域网内的文件下载,只要知道对方资源的url就可以下载,url类似于:http://192.168.0.12:8080/downfiles/xx.doc,只要传过来这个路径就可以下载了,先谢谢各位了
展开
1个回答
展开全部
我回答的 记的要给我分 不然我会哭的
写一个SERVLET
try{
String text = this.servlet.getServletContext().getRealPath("/")+"upload\\"+fileName;
/*读取文件*/
File file = new File(text);
/*如果文件存在*/
if (file.exists()) {
String filename="";
try {
filename = URLEncoder.encode(fileName, enc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.reset();
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment; filename=\"" + filename+ "\"");
int fileLength = (int) file.length();
response.setContentLength(fileLength);
/*如果文件长度大于0*/
if (fileLength != 0) {
/*创建输入流*/
InputStream inStream=null;
try {
inStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
}
byte[] buf = new byte[4096];
/*创建输出流*/
ServletOutputStream servletOS=null;
try {
servletOS = response.getOutputStream();
int readLength;
try {
while (((readLength = inStream.read(buf)) != -1))
{
servletOS.write(buf, 0, readLength);
}
} catch (IOException e) {
e.printStackTrace();
}
inStream.close();
servletOS.flush();
servletOS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
{
throw new SQLException("在服务器上找不到该文件!");
}
/******************************************/
done = true;
}
catch(SQLException ex)
{
done = false;
request.setAttribute("result", "文件下载失败!" + ex.getMessage());
}
if(!done){
return mapping.findForward("error");
}
写一个SERVLET
try{
String text = this.servlet.getServletContext().getRealPath("/")+"upload\\"+fileName;
/*读取文件*/
File file = new File(text);
/*如果文件存在*/
if (file.exists()) {
String filename="";
try {
filename = URLEncoder.encode(fileName, enc);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
response.reset();
response.setContentType(contentType);
response.addHeader("Content-Disposition", "attachment; filename=\"" + filename+ "\"");
int fileLength = (int) file.length();
response.setContentLength(fileLength);
/*如果文件长度大于0*/
if (fileLength != 0) {
/*创建输入流*/
InputStream inStream=null;
try {
inStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
}
byte[] buf = new byte[4096];
/*创建输出流*/
ServletOutputStream servletOS=null;
try {
servletOS = response.getOutputStream();
int readLength;
try {
while (((readLength = inStream.read(buf)) != -1))
{
servletOS.write(buf, 0, readLength);
}
} catch (IOException e) {
e.printStackTrace();
}
inStream.close();
servletOS.flush();
servletOS.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
else
{
throw new SQLException("在服务器上找不到该文件!");
}
/******************************************/
done = true;
}
catch(SQLException ex)
{
done = false;
request.setAttribute("result", "文件下载失败!" + ex.getMessage());
}
if(!done){
return mapping.findForward("error");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询