java文件下载路径_【java文件处理】java项目路径下的文件下载处理

1. controller类:

package com.neo.controller;

import javax.servlet.http.HttpServletResponse;

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

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

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

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

/**

* 通用文件处理controller

* @author wjqhuaxia

*/

@RestController

@RequestMapping("/generalFile")

public class GeneralFileController {

/**

* 模版文件下载

* @param fileType 文件类型 对应 TemplateFileTypeEnum 的key

* @param notIE 是否IE浏览器,true是,false不是

* @return

*/

@GetMapping("/downTemplate/{fileType}/{notIE}")

public String downTemplate(HttpServletResponse response,

@PathVariable String fileType, @PathVariable boolean notIE){

TemplateFileTypeEnum fileTypeEnum = TemplateFileTypeEnum.getEnum(fileType);

FileUtils.downloadFile(response, fileTypeEnum, notIE);

return "下载成功!";

}

// 测试:http://localhost:8080/generalFile/downTemplate/test/false

}

2. 文件工具类

package com.neo.controller;

import java.io.BufferedInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.core.io.ClassPathResource;

/**

* 文件工具类

* @author wjqhuaxia

*/

public class FileUtils {

/**

* 下载项目根目录下doc下的文件

* @param response response

* @param fileName 文件名

* @param notIE 是否IE浏览器,true是,false不是

* @return 返回结果 成功或者文件不存在

*/

public static String downloadFile(HttpServletResponse response, TemplateFileTypeEnum fileTypeEnum, boolean notIE) {

String fileName = fileTypeEnum.getFileName();

String filePath = fileTypeEnum.getFilePath();

String contentType = fileTypeEnum.getContentType();

ClassPathResource classPathResource = new ClassPathResource(filePath + fileName);

InputStream stream = null;

try {

stream =classPathResource.getInputStream();

} catch (IOException e3) {

e3.printStackTrace();

}

response.setHeader("content-type", "application/octet-stream");

response.setContentType(contentType);

try {

String name = java.net.URLEncoder.encode(fileName, "UTF-8");

if (notIE) {

name = java.net.URLDecoder.decode(name, "ISO-8859-1");

}

response.setHeader("Content-Disposition", "attachment;filename=" + name );

} catch (UnsupportedEncodingException e2) {

e2.printStackTrace();

}

byte[] buff = new byte[1024];

BufferedInputStream bis = null;

OutputStream os = null;

try {

os = response.getOutputStream();

bis = new BufferedInputStream(stream);

int i = bis.read(buff);

while (i != -1) {

os.write(buff, 0, buff.length);

os.flush();

i = bis.read(buff);

}

} catch (FileNotFoundException e1) {

//e1.getMessage()+"系统找不到指定的文件";

return "系统找不到指定的文件";

}catch (IOException e) {

e.printStackTrace();

} finally {

if (bis != null) {

try {

bis.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return "success";

}

}

3. 枚举类

package com.neo.controller;

/**

* 模版文件类型枚举类

* @author wjqhuaxia

*/

public enum TemplateFileTypeEnum {

/**

* 测试试例

*/

TEMPLATE_TEST("test","测试模版.xlsx","static/templates/","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

private TemplateFileTypeEnum(String fileType,String fileName,

String filePath,String contentType){

this.contentType = contentType;

this.fileName = fileName;

this.fileType = fileType;

this.filePath = filePath;

}

/**

* 文件类型

*/

private String fileType;

/**

* 文件名

*/

private String fileName;

/**

* 文件所在项目路径的位置

*/

private String filePath;

/**

* response中设置的contentType

*/

private String contentType;

public String getFileType() {

return fileType;

}

public String getFileName() {

return fileName;

}

public String getFilePath() {

return filePath;

}

public String getContentType() {

return contentType;

}

/**

* 通过文件类型获取对应枚举

* @param fileType 文件类型

* @return

*/

public static TemplateFileTypeEnum getEnum(String fileType){

TemplateFileTypeEnum[] values = TemplateFileTypeEnum.values();

for (TemplateFileTypeEnum templateFileTypeEnum : values) {

if(templateFileTypeEnum.getFileType().equals(fileType)){

return templateFileTypeEnum;

}

}

return null;

}

}

说明:

一、如果只是需要进行简单的静态资源访问,直接使用springboot提供的机制即可,不需要动代码。

二、案例中的文件路径如下:

c856924de008c489b9c595763a67025a.png

本案例参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值