新建io.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/testup/uploadBatchFile.do" method="post" enctype="multipart/form-data">
<input type="file" name="files">
<input type="file" name="files">
<button type="submit">上传</button>
</form>
<form action="${pageContext.request.contextPath}/download.do" method="post">
<input name="files" value="SAP接口说明V2.0(1).xlsx">
<button type="submit">下载</button>
</form>
</body>
</html>

  //文件上传控制类 UploadController

package com.zjn.IO;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
@RequestMapping("/testup")
public class UploadController {
/**
* 9.②多个文件上传
*/
@RequestMapping(value="/uploadBatchFile",method=RequestMethod.POST,consumes="multipart/form-data")
public String uploadBatchFile(@RequestParam MultipartFile[] files,RedirectAttributes redirectAttributes){
boolean isEmpty=true;
try {
for (MultipartFile multipartFile : files) {
if(multipartFile.isEmpty()){
continue;
}
String time=new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());
String fileName=multipartFile.getOriginalFilename();
String destFileName="D:\\whupload"+File.separator+time+"_"+fileName;
File destFile=new File(destFileName);
multipartFile.transferTo(destFile);
isEmpty=false;
}
//int i=1/0;
//localhost:8086/test/index?message=upload file success
//redirectAttributes.addAttribute("message","upload file success."); } catch (Exception e) {
// TODO Auto-generated catch block
redirectAttributes.addFlashAttribute("message", "文件上传失败");
System.out.println(e.getMessage());
return "redirect:message.do";
}
if(isEmpty){
redirectAttributes.addFlashAttribute("message", "上传文件为空");
}else{
redirectAttributes.addFlashAttribute("message", "文件上传成功");
}
return "redirect:message.do";
}
@RequestMapping("/message")
public String message() {
return "message";
}
/**
* @Method: makeFileName
* @Description: 生成上传文件的文件名,文件名以:uuid+"_"+文件的原始名称
* @param filename 文件的原始名称
* @return uuid+"_"+文件的原始名称
*/
private String makeFileName(String filename){ //2.jpg
//为防止文件覆盖的现象发生,要为上传文件产生一个唯一的文件名
return UUID.randomUUID().toString() + "_" + filename;
} }

  //文件下载控制类 DownController

package com.zjn.IO;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DownController {
@RequestMapping(value = "/download")
public String download(HttpServletResponse response, Model model,String files) throws Exception {
System.out.println("进入文件下载》》》》》》》》》》》》》》》》》》files==="+files);
files = new String(files.getBytes("iso8859-1"),"UTF-8");//将iso8859-1编码换成utf-8
System.out.println("转码后files===="+files);
//通过文件名找出文件的所在目录
String URL = "D:\\whupload\\"+files;
//得到要下载的文件
File file = new File(URL);
//如果文件不存在
if(!file.exists()){
//如果文件不存在,进行处理
int i=1/0;//系统会报错,除数不能为0.
// return "modules/cms/front/themes/"+site.getTheme()+"/frontError";
}
InputStream inputStream = null;
OutputStream outputStream = null;
//创建缓冲区
byte[] b= new byte[1024];
int len = 0;
try {
//读取要下载的文件,保存到文件输入流
inputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
response.setContentType("application/force-download");
String filename = file.getName();
//设置响应头,控制浏览器下载该文件
response.addHeader("Content-Disposition","attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentLength( (int) file.length( ) );
//循环将输入流中的内容读取到缓冲区当中
while((len = inputStream.read(b)) != -1){
//输出缓冲区的内容到浏览器,实现文件下载
outputStream.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(inputStream != null){
try {
inputStream.close();
inputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStream != null){
try {
outputStream.close();
outputStream = null;
} catch (IOException e) {
e.printStackTrace();
}
}
} return null;
}
}

  

java中io流实现文件上传下载的更多相关文章

  1. JAVA中使用FTPClient实现文件上传下载

    在JAVA程序中,经常需要和FTP打交道,比如向FTP服务器上传文件.下载文件,本文简单介绍如何利用jakarta commons中的FTPClient(在commons-net包中)实现上传下载文件 ...

  2. JAVA中使用FTPClient实现文件上传下载实例代码

    一.上传文件 原理就不介绍了,大家直接看代码吧 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  3. Java 客户端操作 FastDFS 实现文件上传下载替换删除

    FastDFS 的作者余庆先生已经为我们开发好了 Java 对应的 SDK.这里需要解释一下:作者余庆并没有及时更新最新的 Java SDK 至 Maven 中央仓库,目前中央仓库最新版仍旧是 1.2 ...

  4. Java实现FTP批量大文件上传下载篇1

    本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量.大文件的上传下载控件.文章在比较了一系列FTP客户库的基础上,就其中一个比 ...

  5. Java实现FTP与SFTP文件上传下载

    添加依赖Jsch-0.1.54.jar <!-- https://mvnrepository.com/artifact/com.jcraft/jsch --> <dependency ...

  6. SpringMVC中使用 MultipartFile 进行文件上传下载及删除

    一:引入必要的包 <!--文件上传--> <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fil ...

  7. java操作FTP,实现文件上传下载删除操作

    上传文件到FTP服务器: /** * Description: 向FTP服务器上传文件 * @param url FTP服务器hostname * @param port FTP服务器端口,如果默认端 ...

  8. java中的文件上传下载

    java中文件上传下载原理 学习内容 文件上传下载原理 底层代码实现文件上传下载 SmartUpload组件 Struts2实现文件上传下载 富文本编辑器文件上传下载 扩展及延伸 学习本门课程需要掌握 ...

  9. java web 文件上传下载

    文件上传下载案例: 首先是此案例工程的目录结构:

随机推荐

  1. copy/b一个隐藏文件的小技巧

    首先找一张图片 再放一个我们想隐藏的东西 压缩一下 新建txt文本文件b.txt,输入这段代码 copy/b namei.jpg + a.rar namei2.jpg 保存,将文件名改为b.bat 双 ...

  2. rest-framework 响应器(渲染器)

    一 作用: 根据 用户请求URL 或 用户可接受的类型,筛选出合适的 渲染组件. 用户请求URL:    http://127.0.0.1:8000/test/?format=json    http ...

  3. Spring Cloud Alibaba 初体验(六) Seata 及结合 MyBatis 与 MyBatis-Plus 的使用

    一.下载与运行 本文使用 Seata 1.1.0:https://github.com/seata/seata/releases Windows 环境下双击 bin/seata-server.bat ...

  4. VS Code 调试树莓派上的python程序

    安装pip install ptvsd 在py文件前面加代码 import ptvsd ptvsd.enable_attach() ptvsd.wait_for_attach() ptvsd.brea ...

  5. vue富文本编辑器vue-quill-editor

    1.下载Vue-Quill-Editor npm install vue-quill-editor --save 2.下载quill(Vue-Quill-Editor需要依赖) npm install ...

  6. java并发编程实战《一》可见性、原子性和有序性

    可见性.原子性和有序性问题:并发编程Bug的源头 核心矛盾:CPU.IO.内存三者之间的速度差异. 为了合理利用 CPU 的高性能,平衡这三者的速度差异,计算机体系结构.操作系统.编译程序都做出了贡献 ...

  7. moviepy1.03音视频剪辑:使用manual_tracking和headblur实现追踪人脸打马赛克

    ☞ ░ 前往老猿Python博文目录 ░ 一.引言 在moviepy官网的案例<Tracking and blurring someone's face>和CSDN的moviepy大神uc ...

  8. 老猿学5G随笔:5G的三大业务场景eMBB、URLLC、mMTC

    5G的三大业务场景eMBB.URLLC.mMTC: eMBB:英文全称Enhanced Mobile Broadband,即增强移动宽带,是利用5G更好的网络覆盖及更高的传输速率来为用户提供更好的上网 ...

  9. 第8.26节 重写Python类中的__getattribute__方法实现实例属性访问捕获

    一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...

  10. 剑指offer二刷——数组专题——数组中出现次数超过一半的数字

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...