File fileOut = new File(yafile); //yafile为文件需要上传到的路径 FileInputStream fis = new FileInputStream(fileIn); FileOutputStream fos = new FileOutputStream(fileOut); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); byte[] b = new byte[(int) fileIn.length()]; //注意此处长度定义,按照文件大小定义,能够好的保证文件完整性 int len = 0; if((len = bis.read(b)) != -1) { bos.write(b, 0, len); } fis.close(); fos.close();
下载:
String wjmc = tswj.getWjmc(); // String wjmc = "ceshi.xls"; try{ response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((wjmc).getBytes(),"ISO-8859-1"));// 设定输出文件头 response.setContentType("multipart/form-data");// 定义输出类型为所有 OutputStream os = response.getOutputStream(); File file = new File(ServletActionContext.getServletContext().getRealPath("/upload")+"\\"+wjmc); InputStream inputStream = new FileInputStream(file); //写文件 byte[] b = new byte[(int)file.length()]; int len = 0; if((len = inputStream.read(b))>0) { os.write(b,0,len); } //关闭输出流 os.close(); inputStream.close(); }catch(Exception e){ e.printStackTrace(); }