java怎么读取本地目录_java用流读取本地目录下的文件

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import org.springframework.stereotype.Controller;

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

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

import org.springframework.web.servlet.ModelAndView;

@Controller

public class FileManagerController {

private static final Logger logger = Logger.getLogger(Thread

.currentThread().getStackTrace()[1].getClassName());

/**

* 文件下载

* @param filename 文件名

* @param request

* @param response

* @return

*/

@RequestMapping( "/component/file/downloadimage/{filename}")

public ModelAndView downloadAppFile(@PathVariable("filename") String filename,

HttpServletRequest request, HttpServletResponse response) {

String clientip = request.getRemoteAddr();

logger.info("[componentfile]收到来自[" + clientip + "]的下载请求。");

String fileFolder = Constants.IMAGE_PATH;

String filePath = fileFolder + filename;

/*logger.debug("资源文件绝对路径:"+filePath+" 文件名:"+fileRename);

String encoding = System.getProperty("file.encoding");

logger.debug("操作系统编码:"+encoding);*/

java.io.BufferedInputStream bis = null;

java.io.BufferedOutputStream bos = null;

try {

long fileLength = new File(filePath).length();

request.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=utf-8");

response.setContentType("application/x-msdownload;");

response.setHeader("Content-disposition", "attachment; filename="

+ new String(filename.getBytes("utf-8"), "ISO8859-1"));

response.setHeader("Content-Length", String.valueOf(fileLength));

bis = new BufferedInputStream(new FileInputStream(filePath));

bos = new BufferedOutputStream(response.getOutputStream());

byte[] buff = new byte[2048];

int bytesRead;

while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {

bos.write(buff, 0, bytesRead);

}

} catch (UnsupportedEncodingException e) {

logger.error("JSP对象中设置了无效编码。");

} catch (FileNotFoundException e) {

logger.error("资源文件:"+filePath+"不存在。");

} catch (IOException e) {

logger.debug("I/O异常。");

} finally {

if (bis != null) {

try {

bis.close();

} catch (IOException e) {

}

}

if (bos != null) {

try {

bos.close();

} catch (IOException e) {

}

}

}

return null;

}

}

public class Constants {

private static final Logger logger = Logger.getLogger(Thread

.currentThread().getStackTrace()[1].getClassName());

//系统配置文件

private static Properties resourcesCfg = null;

static{

try {

resourcesCfg = new Properties();

FileInputStream fis = new FileInputStream(

java.net.URLDecoder.decode(

Constants.class.getResource("/").getFile()

+"/config.properties",

"utf-8"));

resourcesCfg.load(fis);

} catch (FileNotFoundException e) {

logger.error("找不到系统配置文件:config.properties");

} catch (IOException e) {

logger.error("读取系统配置文件[config.properties]出错.");

}

}

//图片存放路径

public final static String IMAGE_PATH = resourcesCfg.getProperty("image_path");

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值