jsp 如何实现文件上传和下载功能?
答案:2 悬赏:40 手机版
解决时间 2021-03-24 02:03
- 提问者网友:
- 2021-03-23 10:24
jsp 如何实现文件上传和下载功能?
最佳答案
- 五星知识达人网友:轻雾山林
- 2021-03-23 12:03
上传:
MyjspForm mf = (MyjspForm) form;// TODO Auto-generated method stub
FormFile fname=mf.getFname();
byte [] fn = fname.getFileData();
OutputStream out = new FileOutputStream("D:\"+fname.getFileName());
Date date = new Date();
String title = fname.getFileName();
String url = "d:\"+fname.getFileName();
Upload ul = new Upload();
ul.setDate(date);
ul.setTitle(title);
ul.setUrl(url);
UploadDAO uld = new UploadDAO();
uld.save(ul);
out.write(fn);
out.close();
下载:
DownloadForm downloadForm = (DownloadForm)form;
String fname = request.getParameter("furl");
FileInputStream fi = new FileInputStream(fname);
byte[] bt = new byte[fi.available()];
fi.read(bt);
//设置文件是下载还是打开以及打开的方式msdownload表示下载;设置字湖集,//主要是解决文件中的中文信息
response.setContentType("application/msdownload;charset=gbk");
//文件下载后的默认保存名及打开方式
String contentDisposition = "attachment; filename=" + "java.txt";
response.setHeader("Content-Disposition",contentDisposition);
//设置下载长度
response.setContentLength(bt.length);
ServletOutputStream sos = response.getOutputStream();
sos.write(bt);
return null;
MyjspForm mf = (MyjspForm) form;// TODO Auto-generated method stub
FormFile fname=mf.getFname();
byte [] fn = fname.getFileData();
OutputStream out = new FileOutputStream("D:\"+fname.getFileName());
Date date = new Date();
String title = fname.getFileName();
String url = "d:\"+fname.getFileName();
Upload ul = new Upload();
ul.setDate(date);
ul.setTitle(title);
ul.setUrl(url);
UploadDAO uld = new UploadDAO();
uld.save(ul);
out.write(fn);
out.close();
下载:
DownloadForm downloadForm = (DownloadForm)form;
String fname = request.getParameter("furl");
FileInputStream fi = new FileInputStream(fname);
byte[] bt = new byte[fi.available()];
fi.read(bt);
//设置文件是下载还是打开以及打开的方式msdownload表示下载;设置字湖集,//主要是解决文件中的中文信息
response.setContentType("application/msdownload;charset=gbk");
//文件下载后的默认保存名及打开方式
String contentDisposition = "attachment; filename=" + "java.txt";
response.setHeader("Content-Disposition",contentDisposition);
//设置下载长度
response.setContentLength(bt.length);
ServletOutputStream sos = response.getOutputStream();
sos.write(bt);
return null;
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯