首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

单个文件下传与上载实现

2013-03-12 
单个文件上传与下载实现??? ??? ?????? span idaccessoryPath_err stylecolor: red/span??? ??

单个文件上传与下载实现
??? ??? ?????? <span id="accessoryPath_err" style="color: red;"></span>
??? ??? ?????? </td>
??? ??? ???? </tr>

?

?

/**
??? ?* 文件下载

????? path:文件路径
??? ?*/

?

??? public? void downloadFile( String path, HttpServletResponse response) {
??????? String filePath = uploadFilePath + path;
??????? String fileName = "";
??? ??? ?? try {
??? ??? ?? if(filePath.lastIndexOf("/") > 0) {
??? ??? ?? fileName = new String(filePath.substring(filePath.lastIndexOf("/")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
??? ??? ?? }else if(filePath.lastIndexOf("\") > 0) {
??? ??? ?? fileName = new String(filePath.substring(filePath.lastIndexOf("\")+1, filePath.length()).getBytes("GB2312"), "ISO8859_1");
??? ??? ?? }

??? ??? ?? }catch(Exception e) {}
??? ??? ?? FileInputStream fs = null;
??? ??? ?? try {
??? ??? ?? fs = new FileInputStream(new File(filePath));
??? ??? ?? }catch(FileNotFoundException e) {
??? ??? ?? e.printStackTrace();
??? ??? ?? return;
??? ??? ?? }
??? ??? ?? response.reset();
??? ??? ?? response.setContentType("APPLICATION/OCTET-STREAM");
??? ??? ?? response.setHeader("Content-Disposition", "attachment; filename="" + fileName + """);
??? ??? ?? int b = 0;
??? ??? ?? try {
??? ??? ?? PrintWriter out = response.getWriter();
??? ??? ?? while((b=fs.read())!=-1) {
??? ??? ?? out.write(b);
??? ??? ?? }
??? ??? ?? fs.close();
??? ??? ?? out.close();
??? ??? ?? }catch(Exception e) {
??? ??? ?? e.printStackTrace();
??? ??? ?? }
??? ??? ? }

?

?

??? action调用:

?

public void downloadAcc(){
??? ??? ? String path = StrutsUtil.getParameter("path");
??? ??? ? super.downloadFile(path, StrutsUtil.getResponse());
??? ??? ?
??? ??? ? int id = (Integer.parseInt(StrutsUtil.getParameter("id")));
??? ??? ? super.getAccessoryDao().updateDownloadCount(id);
??? ? }

热点排行