Kindeditor快速上手2—将图片附件保存到本地磁盘

将图片附件上传到本地实例

1、定义属性文件rc.properties

file.root=d:/attached/

2、读取属性文件的工具类 PropertiesUtil.java

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);
    }
    

}

3、修改upload_json.do和file_manager_json.do方法中读取的路径

    3.1 upload_json.do

//文件保存目录路径
String savePath = PropertiesUtil.getString("file.root")+ "/";
//文件保存目录URL  
String saveUrl = request.getContextPath() + savePath.substring(2);

    3.2、file_manager_json.do

//根目录路径,可以指定绝对路径,比如 /var/www/attached/
String rootPath = PropertiesUtil.getString("file.root");
//根目录URL,可以指定绝对路径,比如 http://www.yoursite.com/attached/  
String rootUrl  = request.getContextPath() + rootPath.substring(2);

4、图片、附件的下载读取

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) {
                    }
                }
            }
        }
    }
}

    注:参考资料

        http://blog.csdn.net/whos2002110/article/details/33739645

        http://my.oschina.net/u/2524145/blog/607704

你可能感兴趣的:(Kindeditor快速上手2—将图片附件保存到本地磁盘)