java 流文件下载_java中io流实现文件上传下载

新建io.jsp

pageEncoding="UTF-8"%>

文件上传

上传

下载

//文件上传控制类 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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值