为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

JAVA上传下载文件

2017-09-30 4页 doc 16KB 24阅读

用户头像

is_983143

暂无简介

举报
JAVA上传下载文件JAVA上传下载文件 文件上传下载 文件上传 /** * @Title: uploadXML * @Description: 文件上传 * @param fis * @param path * @param name * 设定文件 * @return void 返回类型 * @throws */ public static void uploadXML(InputStream fis, String path, String name) { FileOutputStream fos = null; ...
JAVA上传下载文件
JAVA上传下载文件 文件上传下载 文件上传 /** * @Title: uploadXML * @Description: 文件上传 * @param fis * @param path * @param name * 设定文件 * @return void 返回类型 * @throws */ public static void uploadXML(InputStream fis, String path, String name) { FileOutputStream fos = null; try { // 1、创建目录 File fileDir = new File(path); if (!fileDir.exists()) { fileDir.mkdir(); } // 2、创建文件 File file = new File(path + File.separator + name); if (!file.exists()) { file.createNewFile(); } // 3、上传文件到服务器 fos = new FileOutputStream(file); byte[] b = new byte[1024]; int j = 0; while ((j = fis.read(b)) > 0) { fos.write(b, 0, j); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (Exception e2) { e2.printStackTrace(); } } } 文件下载 /** * @Title: DownloadXML * @Description: 文件下载 * @param response * @param path * @param name * 设定文件 * @return void 返回类型 * @throws */ public static void downloadXML(HttpServletResponse response, String path,String name) { InputStream fis = null; OutputStream os = null; File file = null; try { String downloadpath = path + File.separator + name; file = new File(downloadpath); response.addHeader("Content-Disposition", "attachment;filename="+ new String(name.getBytes("gb2312"), "ISO8859-1")); response.addHeader("Content-Length", "" + file.length()); fis = new BufferedInputStream(new FileInputStream(downloadpath)); os = new BufferedOutputStream(response.getOutputStream()); byte[] b = new byte[1024]; int j = 0; while((j=fis.read(b))>0){ os.write(b, 0, j); } os.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (os != null) { os.close(); } if (file.exists()) { file.delete(); } } catch (IOException e) { e.printStackTrace(); } } }
/
本文档为【JAVA上传下载文件】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索