UploadBiz.java 1.46 KB
package com.cjs.cms.biz.site;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 上传图片公用
 * 
 * @author 孔明可
 *
 */
@Service
public class UploadBiz {

    @Value("${upload.article}")
    public String uploadPath;
    // 上传服务器路径
    @Value("${host}")
    public String imageUrl;

    // 创建图片存储文件夹
    public Map<String, String> markFile() {
        Calendar date = Calendar.getInstance();
        String imageFile = "";
        if (date.get(Calendar.MONTH) + 1 < 10) {
            imageFile = uploadPath + date.get(Calendar.YEAR) + 0 + (date.get(Calendar.MONTH) + 1);
        } else {
            imageFile = uploadPath + date.get(Calendar.YEAR) + (date.get(Calendar.MONTH) + 1);
        }
        File file = new File(imageFile);
        if (!file.exists() && file.isDirectory()) {
            file.mkdir();
        }
        String host = "";
        if (date.get(Calendar.MONTH) + 1 < 10) {
            host = imageUrl + "img/" + date.get(Calendar.YEAR) + 0 + (date.get(Calendar.MONTH) + 1);
        } else {
            host = imageUrl + "img/" + date.get(Calendar.YEAR) + (date.get(Calendar.MONTH) + 1);
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("uploadPath", imageFile);
        map.put("imageUrl", host);
        return map;
    }

}