上傳:
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);
//設(shè)置文件是下載還是打開以及打開的方式msdownload表示下載;設(shè)置字湖集,//主要是解決文件中的中文信息?
response.setContentType("application/msdownload;charset=gbk");
//文件下載后的默認(rèn)保存名及打開方式
String contentDisposition = "attachment; filename=" + "java.txt";
response.setHeader("Content-Disposition",contentDisposition);
//設(shè)置下載長(zhǎng)度
response.setContentLength(bt.length);
ServletOutputStream sos = response.getOutputStream();
sos.write(bt);
return null;