java文件下载框架_JAVA 框架-Springmvc-文件上传与下载

需要的包:spring的21个包,commons-fileupload/io/logging的三个包,标准标签库2个包

异常处理:如果报BeanCreationException: Lookup method resolution failed 可能是缺少必须的包;

spring-servlet.xml配置:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

文件上传页:

pageEncoding="UTF-8"%>

上传文件页面

查看上传文件

文件下载页:

pageEncoding="UTF-8"%>

下载文件页面

上传文件

${f }

${f }

文件控制器类:

package com.hanqi.controller;

import java.io.File;

import java.io.IOException;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;

import org.springframework.http.HttpHeaders;

import org.springframework.http.HttpStatus;

import org.springframework.http.MediaType;

import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.multipart.MultipartFile;

@Controller

@RequestMapping("/file")

public class FileController {

@RequestMapping("/upload")

public String upload(MultipartFile file,HttpServletRequest request) {

System.out.println(file.getName());// input控件中name属性的值

System.out.println(file.getSize()); // 文件大小

System.out.println(file.getOriginalFilename());// 文件名

//在WebContent下新建一个files文件夹,并获取其物理路径

String path = request.getServletContext().getRealPath("/files");

//新建一个空的文件,File.separator相当于"/",同时加上一个时间戳,用于区分相同名字的文件

File orgFile = new File(path + File.separator + new Date().getTime()+"-" + file.getOriginalFilename());

try {

//将接收到的文件放到空的文件中

//该文件实际存放在D:\eclipse workspace\.metadata\.plugins\org.eclipse.wst.server.core

//\tmp0\wtpwebapps\spring-file\files

file.transferTo(orgFile);

} catch (IllegalStateException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return "success";

}

@RequestMapping("/download")

public ResponseEntity testDownLoad(

HttpServletRequest request,

String filename) throws Exception {

// String orgFilename = new String(filename.getBytes("iso-8859-1"), "utf-8");

String path =

request.getServletContext().getRealPath("/files");

// File orgFile = new File(path + "/" + orgFilename);

File orgFile = new File(path + "/" + filename);

// 设置请求头信息

HttpHeaders hh = new HttpHeaders();

// 告诉前台, 以(attachement, 就是下载)的方式打开文件

hh.setContentDispositionFormData("attachment", new String(filename.getBytes("utf-8"), "iso-8859-1"));

// 以二进制流的形式传输文件, 这是最常见的下载方式

hh.setContentType(MediaType.APPLICATION_OCTET_STREAM);

return new ResponseEntity(FileUtils.readFileToByteArray(orgFile), hh,

HttpStatus.CREATED);

}

}

页面跳转控制类:

package com.hanqi.controller;

import java.io.File;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class FormController {

@RequestMapping("/{path}")

public String toPage(@PathVariable("path")String page,Model model,HttpServletRequest request) {

if("download".equals(page)) {

String path = request.getServletContext().getRealPath("/files");

File orgFiles = new File(path);

File[] files = orgFiles.listFiles();

List fileList = new ArrayList();

for(File f:files) {

fileList.add(f.getName());

}

model.addAttribute("fileList", fileList);

}

return page;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值