读取本地文件java

package cn.howso.deeplan.son.feeder.controller.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.howso.deeplan.son.feeder.service.fightgisservice.FightGisService;
import cn.howso.framework.utils.Constants;
import cn.howso.framework.utils.TemplateDownLoadUtils;

@Controller
@RequestMapping("common")
public class CommonController {

	@Value("#{prop['projectImgSrc']}")
	private String projectImgSrc;
	
	@Value("#{prop['exportExcel']}")
	private String exportExcel;
	
	//虚拟路侧截屏图片路经
	@Value("#{prop['projectPrintScreen']}")
	private String projectPrintScreen;
	
	/* 日志 */
	private static Logger logger = LoggerFactory.getLogger(CommonController.class);

//	 private final String filePath = "H:/kk/";

//	private final String filePath = "/home/deeplan/algorithm_file/20180402/";
//	private final String filePath = projectImgSrc;不能直接赋值,直接用projectImgSrc即可

	/**
	 * 加载远程图片 <功能详细描述>
	 * 
	 * @param imgPath
	 * @param type
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/loadImgFile")
	public ModelAndView loadImgFile(String imgPath) {
		// type=0图片文件
		Integer type = 0;

		Map model = new HashMap();
		model.put("path", projectImgSrc + imgPath);
		model.put("type", type);

		return new ModelAndView("load_file", model);
	}
	
	/**
	 * 加载远程图片 <虚拟路测图片>
	 * 
	 * @param imgPath
	 * @param type
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
//	@RequestMapping("/loadScreenImg")
//	public ModelAndView loadScreenImg(String imgPath) {
//		// type=0图片文件
//		Integer type = 0;
//
//		Map model = new HashMap();
//		model.put("path", "/Users/shenrongrong/Desktop/" + imgPath);
//		model.put("type", type);
//
//		return new ModelAndView("load_file", model);
//	}
	/**
	 * 加载远程图片 <虚拟路测图片>
	 * 
	 * @param imgPath
	 * @param type
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/loadSImg")
	public ModelAndView loadSImg(String imgPath) {
		// type=0图片文件
		Integer type = 0;

		Map model = new HashMap();
		model.put("path", imgPath);
		model.put("type", type);

		return new ModelAndView("load_file", model);
	}
}

<%@page import="java.io.*"%>
<%
	Object paramPath = request.getAttribute("path");

	Object paramType = request.getAttribute("type");

	String path = String.valueOf(paramPath);

	String type = String.valueOf(paramType);

	System.out.println(paramPath);

	if (path != null && !"".equals(path.trim()) && type != null) {
		// 如果是图片
		if ("0".equals(type)) {
			response.setContentType("image/jpeg");
		}
		// 如果是音频
		else if ("1".equals(type)) {
			response.setContentType("audio/mp3");
		}
		// 如果是视频
		else if ("2".equals(type)) {
			response.setContentType("video/x-msvideo");
		}
		//excel文件
		else if ("3".equals(type)) {
			response.setContentType("application/vnd.ms-excel");
		}
		/*  path = path.toLowerCase(); */

		String pathSuffix = path.toLowerCase();

		/*start 限制文件路径   2016年4月23日10:44:54  */
		if (!pathSuffix.contains("../") && !pathSuffix.contains(" ") && !pathSuffix.contains("%00")) {
			if (pathSuffix.endsWith(".png") || pathSuffix.endsWith(".jpg") || pathSuffix.endsWith(".ico")
					|| pathSuffix.endsWith(".cebx") || pathSuffix.endsWith(".bmp")
					|| pathSuffix.endsWith(".xls") || pathSuffix.endsWith(".xlsx")) {
				/*end 限制文件路径   2016年4月23日10:44:54  */
				FileInputStream inputStream = null;
				ServletOutputStream outputStream = null;
				try {
					inputStream = new FileInputStream(new File(path)); 
					outputStream = response.getOutputStream();

					byte[] buff = new byte[1024 * 4];

					for (int length = inputStream.read(buff); length != -1; length = inputStream.read(buff)) {
						outputStream.write(buff, 0, length);
					}
					outputStream.flush();
				} catch (Exception e) {
					System.out.println("e1");
					e.printStackTrace();
				} finally {
					out.clear();
					out = pageContext.pushBody();

					try {
						if (null != inputStream) {

							inputStream.close();
						}

					} catch (Exception ee) {
						System.out.println("e2");
						ee.printStackTrace();
					} finally {
						try {
							if (null != outputStream) {
								outputStream.close();
							}
						} catch (Exception e) {
							System.out.println("e3");
							e.printStackTrace();
						}
					}
				}
			}
		}

	}
%>

猜你喜欢

转载自blog.csdn.net/u010251897/article/details/80647896
今日推荐