那个有下载的源代码啊

tianshengling 2008-06-17 04:42:42
谁能给一个下载的源程序
...全文
104 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianshengling 2008-06-20
  • 打赏
  • 举报
回复
谢4#的,帮了个大忙,我已经搞成功了 ,万分感谢
Adam43046721 2008-06-18
  • 打赏
  • 举报
回复
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.zkjy.struts.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
* MyEclipse Struts
* Creation date: 03-28-2008
*
* XDoclet definition:
* @struts.action
*/
public class DownloadAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
String filename;
String path;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)throws Exception {
// TODO Auto-generated method stub

request.setCharacterEncoding("utf-8");
filename=request.getParameter("filename");
path = servlet.getServletContext().getRealPath("/fileupload")+"/"+filename;
downfile(response);
return mapping.findForward("index");
}
public void downfile(HttpServletResponse response) throws IOException{
String f;
if(filename!=null)
{
File file=new File(path);

if(!file.exists()) throw new IOException("文件不存在:"+file.getPath());
else{
f=toUTF8String(filename);
response.setContentType("application/x-msdownload");
response.setContentLength((int)file.length());
response.setHeader("Content-Disposition", "attachment;filename="+f);
write(response.getOutputStream());
}
}
}
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 e){System.out.println(e);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());
}
}
}
return sb.toString();
}
protected void write(OutputStream os) throws IOException {
FileInputStream fis = new FileInputStream(path);
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] s = new byte[1024];
int i = 0;
while ((i = bis.read(s)) > 0) {
os.write(s, 0, i);
}
if (os != null) {
os.flush();
os.close();
}
if (bis != null)
bis.close();
if (fis != null)
fis.close();
}
}
nanjg 2008-06-18
  • 打赏
  • 举报
回复



啥源码啊
kofren 2008-06-18
  • 打赏
  • 举报
回复
只用过DownLoadAction介意楼主看下......代码就懒得发了
dudu3052 2008-06-18
  • 打赏
  • 举报
回复
分 给 我一分啊
tianshengling 2008-06-18
  • 打赏
  • 举报
回复
能把jsp贴出来吗?
tianshengling 2008-06-18
  • 打赏
  • 举报
回复
谢楼上的 ,我试看。

67,538

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧