| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 730 人关注过本帖
标题:哪位大哥大姐有上传和下载文件代码的分享一下,谢谢拉!
只看楼主 加入收藏
秋日
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-6-20
收藏
 问题点数:0 回复次数:4 
哪位大哥大姐有上传和下载文件代码的分享一下,谢谢拉!

哪位大哥大姐有上传和下载文件代码的分享一下,谢谢拉!

搜索更多相关主题的帖子: 大姐 文件 代码 分享 
2007-06-20 13:46
guoxhvip
Rank: 8Rank: 8
来 自:聖西羅南看臺
等 级:贵宾
威 望:44
帖 子:4052
专家分:135
注 册:2006-10-8
收藏
得分:0 
我也想要 但是是在Servlet里实现的那种

愛生活 && 愛編程
2007-06-20 23:47
kingyor
Rank: 1
等 级:新手上路
帖 子:66
专家分:0
注 册:2006-6-4
收藏
得分:0 
你可以在网上下载一个文件管理系统,网上有很多,里面就有上传和下载的实例。

宝哥啊宝哥~~命苦啊命苦啊~~~
2007-06-21 09:31
pilou5400
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-11-7
收藏
得分:0 
我有一个上床下载的代码!

上床:
你的类{
if(!emailForm.getFile().getFileName().equals("")){
String savePath = "/Jsp_File/ll/Email/upload/";
//读取文件
FormFile file = emailForm.getFile();
//设置保存路径
String lastName = file.getFileName().substring(file.getFileName().length()-4);
String upLoadName = getThisDate() + lastName;
emailForm.setFILE_PATH(savePath + upLoadName);
//emailForm.setFILE_PATH(savePath + file.getFileName());
try {
InputStream stream = file.getInputStream();//把文件读入
String filePath = servlet.getServletContext().getRealPath(savePath);//取当前系统路径
OutputStream bos = new FileOutputStream(filePath +"/"+
upLoadName);//建立一个上传文件的输出流
System.out.println(filePath+"/"+upLoadName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
}else{
emailForm.setFILE_PATH("");
}
}

/*下载*/
public ActionForward download(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//String fileName = request.getParameter("filePath");
System.out.println("select file_path from t_email where email_id = " + request.getParameter("emailid"));
Map filePathMap = Conn.executeQueryById("select file_path from t_email where email_id = " + request.getParameter("emailid"));
System.out.println(filePathMap.get("file_path").toString());

String fileName = filePathMap.get("file_path").toString();

System.out.println(fileName);
fileName = fileName==null?"":fileName;
fileName = fileName.trim();
InputStream inStream= null;
String attch_name = "";
byte[] b = new byte[100];
int len= 0;
try {
//取得附件的名称
attch_name = getAttachName(fileName);

fileName = getRealName(request,fileName);
if(fileName==null) {
request.setAttribute("msg","文件不存在");
request.getRequestDispatcher(request.getContextPath()+"/Jsp_File/cch/error/error.jsp").forward(request,response);
return null;
}
attch_name = toUtf8String(attch_name);
//读到流中
inStream=new FileInputStream(fileName);
//设置输出的格式
response.reset();
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition","attachment; filename=\"" + attch_name + "\"");
//循环取出流中的数据
while((len=inStream.read(b)) >0) {
response.getOutputStream().write(b,0,len);
}
inStream.close();
}catch ( Exception e ){
if ( e instanceof java.io.FileNotFoundException ) {
try {
request.setAttribute("msg","下载文件出错");
request.getRequestDispatcher(request.getContextPath()+"/Jsp_File/cch/error/error.jsp").forward(request,response);
}catch (ServletException e1) {
e1.printStackTrace();
}catch ( IOException ex ) {
ex.printStackTrace(System.err);
}
}else {
e.printStackTrace(System.err);
}
}
return null;
}

/*下载*/
public static String getAttachName(String file_name) {
if(file_name==null) return "";
file_name = file_name.trim();
int iPos = 0;
iPos = file_name.lastIndexOf("\\");
if(iPos>-1){
file_name = file_name.substring(iPos+1);
}
iPos = file_name.lastIndexOf("/");
if(iPos>-1){
file_name = file_name.substring(iPos+1);
}
iPos = file_name.lastIndexOf(File.separator);
if(iPos>-1){
file_name = file_name.substring(iPos+1);
}
return file_name;
}
/*下载*/
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i=0;i<s.length();i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) k += 256;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
String s_utf8 = sb.toString();
sb.delete(0,sb.length());
sb.setLength(0);
sb = null;
return s_utf8;
}

2007-06-21 21:20
秋日
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2007-6-20
收藏
得分:0 
谢谢楼上的,我试一下要是不行的话还是要麻烦大家。

世上本没有路,走的人多了也就成了路。
2007-06-22 17:32
快速回复:哪位大哥大姐有上传和下载文件代码的分享一下,谢谢拉!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013596 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved