社区
Web 开发
帖子详情
急!!如何实现恩见下载?
dalong106
2003-09-11 12:48:07
请问在jsp页面中如何能实现文件下载功能?急求!谢谢!
...全文
32
9
打赏
收藏
急!!如何实现恩见下载?
请问在jsp页面中如何能实现文件下载功能?急求!谢谢!
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
9 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
kevincom
2003-09-11
打赏
举报
回复
<%@ page language="java" contentType="text/html" %>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<%
try{// Initialization
mySmartUpload.initialize(pageContext);
// With options
mySmartUpload.setContentDisposition("attachment;");
mySmartUpload.downloadFile(file,null,strShowName);
//file应该是文件文件路径及文件名如:"../upload/setup.rar";strShowName为另存为的文件名
}catch(Exception e){
System.err.println("文件下载错误"+e.getMessage());
}
%>
pqds
2003-09-11
打赏
举报
回复
很全吧!呵呵!这是人家总结的
pqds
2003-09-11
打赏
举报
回复
Java中对文件的操作
java中提供了io类库,可以轻松的用java实现对文件的各种操作。下面就来说一下如何用java来实现这些操作。
1。新建目录
<%@ page contentType="text/html;charset=gb2312"%>
<%
String filePath="c:/aaa/";
filePath=filePath.toString();//中文转换
java.io.File myFilePath=new java.io.File(filePath);
if(!myFilePath.exists())
myFilePath.mkdir();
%>
2。新建文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%
String filePath="c:/哈哈.txt";
filePath=filePath.toString();
File myFilePath=new File(filePath);
if(!myFilePath.exists())
myFilePath.createNewFile();
FileWriter resultFile=new FileWriter(myFilePath);
PrintWriter myFile=new PrintWriter(resultFile);
String strContent = "中文测试".toString();
myFile.println(strContent);
resultFile.close();
%>
3。删除文件
<%@ page contentType="text/html;charset=gb2312"%>
<%
String filePath="c:/支出证明单.xls";
filePath=filePath.toString();
java.io.File myDelFile=new java.io.File(filePath);
myDelFile.delete();
%>
4。文件拷贝
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
int bytesum=0;
int byteread=0;
file://读到流中
InputStream inStream=new FileInputStream("c:/aaa.doc");
FileOutputStream fs=new FileOutputStream( "d:/aaa.doc");byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
inStream.close();
%>
5。整个文件夹拷贝
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.io.*" %>
<%String url1="C:/aaa";
String url2="d:/java/";
(new File(url2)).mkdirs();
File[] file=(new File(url1)).listFiles();
for(int i=0;i<file.length;i++){
if(file[i].isFile()){
file[i].toString();
FileInputStream input=new FileInputStream(file[i]);
FileOutputStream output=new FileOutputStream(url2+"/"+(file[i].getName()).toString());
byte[] b=new byte[1024*5];
int len;
while((len=input.read(b))!=-1){
output.write(b,0,len);
}
output.flush();
output.close();
input.close();
}
}
%>
6。文件下载
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.io.*" %>
<%
String fileName = "zsc104.swf".toString();
//读到流中
InputStream inStream=new FileInputStream("c:/zsc104.swf");
//设置输出的格式
response.reset();
response.setContentType("bin");
response.addHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
//循环取出流中的数据
byte[] b = new byte[100];
int len;
while((len=inStream.read(b)) >0)
response.getOutputStream().write(b,0,len);
inStream.close();
%>
7。数据库字段中的文件下载
<%@ page contentType="text/html; charset=gb2312" %>
<%@ page import="java.sql.*"%>
<%@ page import="java.lang.*" %>
<%@ page import="java.io.*" %>
<%@ page import="com.jspsmart.upload.*" %>
<%@ page import="DBstep.iDBManager2000.*"%>
<%
int bytesum=0;
int byteread=0;
//打开数据库
ResultSet result=null;
String Sql=null;
PreparedStatement prestmt=null;
DBstep.iDBManager2000 DbaObj=new DBstep.iDBManager2000();
DbaObj.OpenConnection();
//取得数据库中的数据
Sql="select * from t_local_zhongzhuan ";
result=DbaObj.ExecuteQuery(Sql);
result.next();
file://将数据库中的数据读到流中
InputStream inStream=result.getBinaryStream("content");
FileOutputStream fs=new FileOutputStream( "c:/dffdsafd.doc");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
%>
8。把网页保存成文件
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%
URL stdURL = null;
BufferedReader stdIn = null;
PrintWriter stdOut = null;
try {
stdURL = new URL("http://www.163.com");
}
catch (MalformedURLException e) {
throw e;
}
try {
stdIn = new BufferedReader(new InputStreamReader(stdURL.openStream()));
stdOut = new PrintWriter(new BufferedWriter(new FileWriter("c:/163.html")));
}
catch (IOException e) {
}
/***把URL指定的页面以流的形式读出,写成指定的文件***/
try {
String strHtml = "";
while((strHtml = stdIn.readLine())!=null) {
stdOut.println(strHtml);
}
}
catch (IOException e) {
throw e;
}
finally {
try {
if(stdIn != null)
stdIn.close();
if(stdOut != null)
stdOut.close();
}
catch (Exception e) {
System.out.println(e);
}
}
%>
9。直接下载网上的文件
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%
int bytesum=0;
int byteread=0;
URL url = new URL("http://pimg.163.com/sms/micheal/logo.gif");
URLConnection conn = url.openConnection();
InputStream inStream = conn.getInputStream();
FileOutputStream fs=new FileOutputStream( "c:/abc.gif");
byte[] buffer =new byte[1444];
int length;
while ((byteread=inStream.read(buffer))!=-1)
{
out.println("<DT><B>"+byteread+"</B></DT>");
bytesum+=byteread;
System.out.println(bytesum);
fs.write(buffer,0,byteread);
}
*>
kevincom
2003-09-11
打赏
举报
回复
用jspSmartUpload啊
runi
2003-09-11
打赏
举报
回复
楼上大姐别着急.我来了,就可以给分,揭帖了!
pqds
2003-09-11
打赏
举报
回复
我靠!
pqds
2003-09-11
打赏
举报
回复
敢问你什么时候给分,俺一个女农民容易吗俺!有如嫖了又不给钱
pqds
2003-09-11
打赏
举报
回复
敢问你什么时候给分,俺一个女农民容易吗俺!
pqds
2003-09-11
打赏
举报
回复
为什么现在都要问两遍!
基于R语言
实现
的运输车辆安全驾驶行为分析源码+数据集+项目说明+超详细注释.zip
一.车辆运输路线及行车状态分析 (一).方法: 数据预处理,我们使用R语言提取10辆车的GPS速度,GPS 里程,采集时间,经纬度等信息来计算每条线路对应的行车里程、平均行车速度、
急
加速
急
减速情况...欢迎
下载
使用
JCraete2005
:)以上就是我在用Jcreator进行JAVA编程总结出来的一点点使用技巧,或许有些朋友已经知道了这里面的功能,班门弄斧,写得不好也请大家见凉!同时我还期望大家能把自己在使用Jcretor过程中总结出来的经验贴上来供...
深度学习入门与实践
学习了很多深度学习理论知识,却不知道如何具体...别
急
,本课程就手把手教你如何构建自己的深度学习应用。从经典的神经网络到时下流行的方法,即便是新手也可以快速入门。就让我们一起在人工智能的浪潮中,扬帆起航吧。
【机器学习基础】Python
实现
行转列?!超简单,赶快get起来
◆ ◆ ◆ ◆ ◆ ... 往期精彩回顾 适合初学者入门人工智能的路线及资料
下载
机器学习及深度学习笔记等资料打印机器学习在线手册深度学习笔记专辑《统计学习方法》的代码复现专辑 AI基础
下载
机器学习的数学基础专辑
php获取笔顺矢量,php如何获取汉字笔画数功能的实例分析
本文实例讲述了PHP获取汉字笔画数功能。分享给大家供大家参考,具体如下:无意中看到这么个东西,用PHP得到汉字的笔画数。以类的方式
实现
,有那么点意思,先留下了。...
Web 开发
81,092
社区成员
341,718
社区内容
发帖
与我相关
我的任务
Web 开发
Java Web 开发
复制链接
扫一扫
分享
社区描述
Java Web 开发
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章