新建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. presto 访问kudu 多schemas配置

    presto需要访问kudu数据源,但是impala可以直接支持多数据库存储,但是presto不能原生支持,按照presto的官网设置了然而并不起作用. 官方文档: 到官方github提问了,然后并没 ...

  2. 二、pandas学习

    1.food.csv ========================================================================================= ...

  3. fist-第七天冲刺随笔

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...

  4. 老猿学5G扫盲贴:N6接口用户平面协议栈对应的网络分层模型

    在网络通信模型中,都对应有分层的网络结构,如开放式系统互联(OSI)的七层模型(物理层.数据链路层.网络层.传输层.会话层.表示层和应用层)以及TCP/IP四层(网络接口层.网络层.传输层和应用层)模 ...

  5. 第10.1节 Python的模块及模块导入

    一. 什么是模块 Python中的模块即单个的Python代码文件,为什么称为模块呢?这是因为在Python中,每个独立的Python文件都可以作为被其他代码导入的模块使用,导入的模块有自己的名字空间 ...

  6. Python正则表达式re.findall一个有趣的现象

    下面通过几个案例来分析一下, 注意:本节的parsematch函数请参考<妙用re.sub分析正则表达式解析匹配过程> 案例一: >>> re.findall(r&quo ...

  7. 课堂练习之疫情APP

    title: 课堂练习之疫情查询APP date: 2020-03-17 20:08:51 tags: 在之前的体温记录APP上改进,只写出疫情信息查询页面的代码. 实体类与上篇博客SSM整合中的Ci ...

  8. 西湖论剑2020MISC-Yusa_yyds

    非常规USB流量分析 附件下载: 链接:https://pan.baidu.com/s/1Gjgj1EH9qmX0PYi21uYlDg 提取码:x9xn 先提取USB流量数据,使用工具: https: ...

  9. sails框架结合mocha的测试环境搭建

    一.环境结构 1.首先最底层是操作系统 2.其次在操作系统之上是Node.js的运行环境,和Database 3.再之上就是sail和mocha框架 二.环境搭建 1.首先需要Node.js的运行环境 ...

  10. WinForm 加载大数据时不闪烁的ListView

    自己之前做小工具的时候,遇到这个问题,记录一下... namespace 相册处理 { //将ListView重载为此新类,解决加载过程中闪烁的问题 //在designer.cs中改写: //priv ...