Java SSM 图片上传

近期写到图片上传,就是将图片上传至服务器

该上传的方式为,前台将图片转成base64码 ,然后将数据流发送后台,然后我再进行base64解码,并且将图片上传至服务器

 

// DTO层

package com.hand.utils.uploads.dto;

import java.util.List;

/**Auto Generated By Hap Code Generator**/


public class Upload {

    private String fileName;//文件名称

    private String imgStr;// 图片流

    private String[] imgListStr;// 批量上传

    private String jsonDataStr;

    public String getJsonDataStr() {
        return jsonDataStr;
    }

    public void setJsonDataStr(String jsonDataStr) {
        this.jsonDataStr = jsonDataStr;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getImgStr() {
        return imgStr;
    }

    public void setImgStr(String imgStr) {
        this.imgStr = imgStr;
    }

    public String[] getImgListStr() {
        imgListStr = this.getImgStr().split(",");
        return imgListStr;
    }

    public void setImgListStr(String[] imgListStr) {
        this.imgListStr = imgListStr;
    }
}
// Controller 层
package com.hand.utils.uploads.controllers;

import com.hand.hap.system.dto.ResponseData;
import com.hand.utils.uploads.dto.Upload;
import com.hand.utils.uploads.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class UploadController{

    @Autowired
    private IUploadService service;


    @RequestMapping(value = "/app/upload/img",method = RequestMethod.POST)
    @ResponseBody
    public ResponseData appUploadImg(Upload dto, HttpServletRequest request) {
        /**
         *
         * 功能描述: app 上传图片
         *
         * @auther:lkj
         * @date:2018/7/26 下午4:40
         * @param:[dto, request]
         * @return:com.hand.hap.system.dto.ResponseData
         *
         */
        try {
            ResponseData responseData = service.appUploadImg(dto);
            return responseData;
        } catch (Exception e) {
            e.printStackTrace();
            ResponseData responseData = new ResponseData(false);
            responseData.setMessage(e.getMessage());
            return responseData;
        }
    }


}
// IUploadService 层
package com.hand.utils.uploads.service;

import com.hand.hap.system.dto.ResponseData;
import com.hand.utils.uploads.dto.Upload;

public interface IUploadService{
    /**
     *
     * 功能描述: APP 图片上传
     *
     * @auther:lkj
     * @date:2018/7/26 下午4:43
     * @param:
     * @return:
     *
     */
    ResponseData appUploadImg(Upload dto) throws Exception;
}

// ServiceImpl 实现类

package com.hand.utils.uploads.service.impl;

import com.hand.hap.system.dto.ResponseData;
import com.hand.utils.components.mapper.UtilMapper;
import com.hand.utils.uploads.dto.Upload;
import com.hand.utils.uploads.service.IUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class UploadServiceImpl implements IUploadService {

    @Autowired
    private UtilMapper utilMapper;

    @Override
    public ResponseData appUploadImg(Upload dto) throws Exception{
        /**
         *
         * 功能描述: App 图片上传
         *
         * @auther:lkj
         * @date:2018/7/26 下午4:43
         * @param:[dto]
         * @return:com.hand.hap.system.dto.ResponseData
         *
         */
        String fileName = dto.getFileName();// 文件夹名称
        String[] imgListStr = dto.getImgListStr();
        //拼接成完整的指定的文件路径名,创建新文件 
        //我的文件路径是配置的快码
        String sql = "SELECT T.PROFILE_VALUE FROM SYS_PROFILE_VALUE T WHERE ";
        sql += "T.PROFILE_ID = ( SELECT PROFILE_ID FROM sys_profile WHERE PROFILE_NAME = 'APP_UP_IMG_PATH' ) ";
        String mysql = utilMapper.mysql(sql);
        String filePath = mysql+"/"+fileName;
        File file = new File(filePath);
        if(!file.exists()){
            file.mkdirs();
        }
        for(int i=0;i<imgListStr.length;i++){
            // 获取时间戳
            long nowDate=(new Date().getTime());
            String filename=nowDate+".jpg";
            GenerateImage(imgListStr[i],filePath+"/"+filename);
        }

        return new ResponseData();
    }
    //解码
    public static boolean GenerateImage(String imgStr, String imgFilePath) throws IOException {
        // 对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(bytes);
            out.flush();
            out.close();
            return true;

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值