文件的上传和下载
1、文件上传

html页面代码如下

<form method="post" action="/file/upload1" enctype="multipart/form-data">
<p>
文件:<input type="file" name="file">
<input type="submit" value="上传">
</p>
</form>
Controller代码,
@RequestMapping(“/upload1”)
@ResponseBody
public String upload1(@RequestParam(“file”)MultipartFile file,HttpServletRequest request) throws IOException {
//获取文件名
String filename=file.getOriginalFilename();
//文件上传后路径
String path=”E://images//“;
File file1=new File(path+filename);
//如果文件不存在,则创建新的文件夹
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
}
byte[] bytes=file.getBytes(); //获得文件字节
//将filename文件写入到path路径下
FileOutputStream fos=new FileOutputStream(file1);
fos.write(bytes); //将字节写入
fos.flush();
fos.close();
return “ok”;
}
判断文件的另一个方法,将
File file1=new File(path+filename);
//如果文件不存在,则创建新的文件夹
if(!file1.getParentFile().exists()){
file1.getParentFile().mkdirs();
}
FileOutputStream fos=new FileOutputStream(file1);
替换成
File file1=new File(path);
//如果文件不存在,则创建新的文件夹
if(!file1.exists()){
file1.mkdirs();
}
FileOutputStream fos=new FileOutputStream(path+filename);

2、文件上传(第二中方法)
  1. @RequestMapping("/upload1")
  2. @ResponseBody
  3. public String upload1(@RequestParam("file")MultipartFile file,HttpServletRequest request) throws IOException {
  4. //获取文件名
  5. String filename=file.getOriginalFilename();
  6. //文件上传后路径
  7. String path="E://images//";
  8. File file1=new File(path+filename);
  9. //如果文件不存在,则创建新的文件夹
  10. if(!file1.getParentFile().exists()){
  11. file1.getParentFile().mkdirs();
  12. }
  13. //上传的文件存放的位置
  14. file.transferTo(file1);
  15. return "ok";
  16. }
3、文件下载代码

//文件下载
@RequestMapping(“/download”)
@ResponseBody
public String download(HttpServletResponse response) throws IOException {
//要下载的文件路径
String filepath=”E://images//“;
//要下载的文件名称
String filename=”1.jpg”;
File file=new File(filepath,filename);
//判断文件是否存在
if(file.exists()){
response.setContentType(“application/force-download”);//设置强制下载不打开
response.addHeader(“Content-Disposition”,”attachment;fileName=”+filename);//设置文件名
byte[]buf=new byte[1024];
//文件输入了
FileInputStream fis=null;
//带缓冲的字节流
BufferedInputStream bis=null;
OutputStream os=null;//输出流
try{
fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);
os=response.getOutputStream();
int i=bis.read(buf);
while (i!=-1){
os.write(buf);
i=bis.read(buf);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bis!=null){
bis.close();
}if(fis!=null){
fis.close();
}
}
}
return “download”;
}

4、多文件上传代码

html页面代码

<form action="/file/uploads" method="post" enctype="multipart/form-data">
<p>文件1:<input type="file" name="file"></p>
<p>文件2:<input type="file" name="file"></p>
<p>文件3:<input type="file" name="file"></p>
<p> <input type="submit" value="上传"></p>
</form>
Controller代码
@RequestMapping(“/uploads”)
@ResponseBody
public String uploads(HttpServletRequest request){
//获取上传的文件
List<MultipartFile>files=((MultipartHttpServletRequest)request).getFiles(“file”);
MultipartFile file=null;
BufferedOutputStream stream=null;
for(int i=0;i<files.size();i++){
file=files.get(i);
if(!file.isEmpty()){
try{
byte[]bytes=file.getBytes();
stream=new BufferedOutputStream(new FileOutputStream(
new File(file.getOriginalFilename())));
stream.write(bytes);
stream.close();
} catch (IOException e) {
return “failure,”+i+”=>”+e.getMessage();
}
}else{
return “failure,is null”;
}
}
return “ok”;
}
总结:以上方法都亲测有效,如果问题请留言。

最新文章

  1. servlet 之 response 回复 字节流 字符流
  2. 关于BeanUiles.copyPropertis()的用法
  3. json 对象 数组
  4. [转]JAVA设计模式之单例模式
  5. LDM和STM指令
  6. isset,empty,is_null小知识
  7. Android 不能返回 parent Activity 的问题
  8. 机器学习 —— 基础整理(八)循环神经网络的BPTT算法步骤整理;梯度消失与梯度爆炸
  9. javascript 之原型、原型链-14
  10. 发放春节福利,ASP.NET Core断点续传
  11. 洛谷P2042 [NOI2005]维护数列
  12. javascript之BOM浏览器对象模型引入
  13. SpriteBuilder中关节的Breaking force属性
  14. 字符串匹配KMP算法的讲解C++
  15. 前端(各种demo)一:css实现三角形,css实现梯形,pop弹层,css伪类before,after使用,svg使用(持续更新中)
  16. vue 高德地图使用 vue-amap
  17. 自学Zabbix12.3 Zabbix命令-zabbix_agentd
  18. 用react脚手架新建项目
  19. [iOS] Win8下在Vmware11中安装使用苹果系统OS X 10.10
  20. css基础之line-height

热门文章

  1. php curl 伪造IP来源的实例代码
  2. innodb分配内存
  3. function参数
  4. PKUWC 2018 铁牌记
  5. WebDriverAPI(3)
  6. Docker学习--基本docker命令
  7. (转) Rabbitmq学习笔记
  8. Netty核心概念(10)之内存管理
  9. 搭建互联网架构学习--006--duboo准备之zk集群部署安装
  10. 搭建互联网架构学习--003--maven以及nexus私服搭建