SpringMvc下载文件

使用springmvn文件上传

添加pom依赖


            commons-fileupload
            commons-fileupload
            1.3.2
        


配置springmvc-servlet.xml

 
    
        
    

form表单


控制器

@RequestMapping("/upload")
public String fileupload(String name,MultipartFile file) {
System.out.println("name:" + name);
System.out.println("OriginalFileName:" + file.getOriginalFilename());
System.out.println("size" + file.getSize());
if(!file.isEmpty()) {
InputStream inputStream = file.getInputStream();
//后面的该会了吧
}
return "";
}

导入excel

二、快速入门

2.1 引入maven依赖



    com.xuxueli
    xxl-excel
    1.0.0

2.2 定义Java对象

@ExcelSheet(name = "商户列表", headColor = HSSFColor.HSSFColorPredefined.LIGHT_GREEN)
public class ShopDTO {

    @ExcelField(name = "商户ID")
    private int shopId;

    @ExcelField(name = "商户名称")
    private String shopName;

    public ShopDTO() {
    }

    public ShopDTO(int shopId, String shopName) {
        this.shopId = shopId;
        this.shopName = shopName;
    }

    public int getShopId() {
        return shopId;
    }

    public void setShopId(int shopId) {
        this.shopId = shopId;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }

}

2.3 Excel导出:Object 转换为 Excel

控制器

/**
 * @author Wgs
 * @version 1.0
 * @create:2017/11/14
 */
@Controller
public class UploadController {
    @PostMapping("/user/upload")
    public String fileupload(String name, MultipartFile file) throws IOException {
        System.out.println("name:" + name);
        System.out.println("OriginalFileName:" + file.getOriginalFilename());
        System.out.println("size" + file.getSize());
        List list = ExcelImportUtil.importExcel(ShopDTO.class, file.getInputStream());
        for (Object result : list) {
            System.out.println(result);
        }
        return "";
    }


}
 
 
// 参考测试代码:com.xuxueli.poi.excel.test.Test

/**
 * Excel导出:Object 转换为 Excel
 */
ExcelExportUtil.exportToFile(shopDTOList, filePath);

2.4 Excel导入:Excel 转换为 Object

// 参考测试代码:com.xuxueli.poi.excel.test.Test

/**
 * Excel导入:Excel 转换为 Object
  */
List list = ExcelImportUtil.importExcel(ShopDTO.class, filePath);
 
 

三、总体设计

3.1 功能定位

XXL-EXCEL 是在 Java 对象和 Excel 文档之间进行转换的迅速而灵活的工具。

3.2 Java 对象 和 Excel映射关系

-- Excel Java 对象
Sheet Java对象列表
表头 Sheet首行 Java对象Field
数据 Sheet一行记录 Java对象列表中一个元素

3.3 核心注解:ExcelSheet

功能:描述Sheet信息,注解添加在待转换为Excel的Java对象类上,可选属性如下。

ExcelSheet 说明
name 表/Sheet名称
headColor 表头/Sheet首行的颜色

3.4 核心注解:ExcelField

功能:描述Sheet的列信息,注解添加在待转换为Excel的Java对象类的字段上,可选属性如下。

ExcelField 说明
name 属性/列名称

四、版本更新日志

4.1 版本 V1.1.x,新特性[2015-12-05]

  • 1、Excel导出:支持Java对象装换为Excel,并且支持File、字节数组、Workbook等多种导出方式;
  • 2、Excel导入:支持Excel转换为Java对象,并且支持File、InputStream、文件路径、Workbook等多种导入方式;

TODO LIST

  • 1、单个Excel多Sheet导出导出;
  • 2、列合并导入导出;
  • 3、行合并导入导出;
  • 4、HSSFWorkbook=2003/xls、XSSFWorkbook=2007/xlsx 兼容支持;

使用SpringMVC优雅的下载文件

 @GetMapping("/download")
    @ResponseBody
    public ResponseEntity downLoadFile(Integer id) throws FileNotFoundException {
        InputStream inputStream = diskService.downloadFile(id);
        Disk disk = diskService.findById(id);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        headers.setContentDispositionFormData("attachement",disk.getSourceName(), Charset.forName("UTF-8"));

        return new ResponseEntity<>(new InputStreamResource(inputStream),headers, HttpStatus.OK);
    }

   @GetMapping("/download")
    publicResponseEntitydownloadFile() throwsFileNotFoundException {
        Filefile = newFile("D:/upload/1.jpg");
        HttpHeadershttpHeaders = newHttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        httpHeaders.setContentLength(file.length());
        httpHeaders.setContentDispositionFormData("attachment", "照片.jpg", Charset.forName("UTF-8"));
        InputStreamResourceinputStreamResource = newInputStreamResource(newFileInputStream(file));
        returnnewResponseEntity(inputStreamResource, httpHeaders, HttpStatus.OK);
    }

你可能感兴趣的:(SpringMvc下载文件)