file.root=d:/attached/
import java.util.ArrayList; import java.util.List; import org.apache.commons.configuration.CompositeConfiguration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; /*********************************** * @ClassName: PropertiesUtil.java * @Description: TODO * @author: renchuan.q * @createdAt: 2014-9-5上午10:45:56 ***********************************/ public class PropertiesUtil { private static CompositeConfiguration config; private static PropertiesConfiguration propertiesConfig; static { config = new CompositeConfiguration(); if (propertiesConfig == null) { try { propertiesConfig = new PropertiesConfiguration("doa.properties"); propertiesConfig.setReloadingStrategy(new FileChangedReloadingStrategy()); config.addConfiguration(propertiesConfig); } catch (ConfigurationException e) { e.printStackTrace(); } } } private PropertiesUtil() { // do nothing } public static String getString(String propertyKey) { return config.getString(propertyKey); } public static String getString(String propertyKey, String defaultValue) { return config.getString(propertyKey, defaultValue); } public static int getInt(String propertyKey) { return config.getInt(propertyKey); } public static int getInt(String key, int defaultValue) { return config.getInt(key, defaultValue); } public static float getFloat(String propertyKey) { return config.getFloat(propertyKey); } public static float getFloat(String propertyKey, float defaultValue) { return config.getFloat(propertyKey, defaultValue); } public static boolean getBoolean(String propertyKey) { return config.getBoolean(propertyKey); } public static boolean getBoolean(String propertyKey, boolean defualtValue) { return config.getBoolean(propertyKey, defualtValue); } public static String[] getStringArray(String propertyKey) { return config.getStringArray(propertyKey); } public static List<String> getStringList(String propertyKey) { List<String> list = new ArrayList<String>(); String[] strArr = getStringArray(propertyKey); for (String value : strArr) { list.add(value); } return list; } @SuppressWarnings("rawtypes") public static List getList(String propertyKey) { return config.getList(propertyKey); } }
//文件保存目錄路徑 String savePath = PropertiesUtil.getString("file.root")+ "/"; //文件保存目錄URL String saveUrl = request.getContextPath() + savePath.substring(2);
//根目錄路徑,能夠指定絕對路徑,好比 /var/www/attached/ String rootPath = PropertiesUtil.getString("file.root"); //根目錄URL,能夠指定絕對路徑,好比 http://www.yoursite.com/attached/ String rootUrl = request.getContextPath() + rootPath.substring(2);
import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import com.dms.erp.common.util.PropertiesUtil; @Controller public class AttachedController { @RequestMapping("/attached/{fileType}/{uploadDate}/{fileName}.{suffix}") public void attached(HttpServletRequest request, HttpServletResponse response, @PathVariable String fileType, @PathVariable String uploadDate, @PathVariable String suffix, @PathVariable String fileName) { // 根據suffix設置響應ContentType // response.setContentType("text/html; charset=UTF-8"); String basePath = PropertiesUtil.getString("file.root"); InputStream is = null; OutputStream os = null; try { File file = new File(basePath + fileType + "/" + uploadDate + "/" + fileName + "." + suffix); is = new FileInputStream(file); byte[] buffer = new byte[is.available()]; is.read(buffer); os = new BufferedOutputStream(response.getOutputStream()); os.write(buffer); os.flush(); } catch (Exception e) { // 判斷suffix // 圖片請求能夠在此顯示一個默認圖片 // file顯示文件已損壞等錯誤提示... } finally { if (is != null) { try { is.close(); } catch (IOException e) { } if (os != null) { try { os.close(); } catch (IOException e) { } } } } } }
注:參考資料html
http://blog.csdn.net/whos2002110/article/details/33739645
java
http://my.oschina.net/u/2524145/blog/607704web