项目总结37:Java上传图片保存到Oracle以及读取图片

1-实体类-封装图片数据

package com.hzsun.shr.web.maintenance.entity.image;

/* *
 *@Description:
 *@Author:TYJ
 *@Date: create in  2019/8/8 14:17
 */


public class ImageCenter{

    private String imageId;
    private byte[] content;//用byte[]接收图片数据

    public ImageCenter() {
    }

    public ImageCenter( byte[] content,String userId) {
        this.imageId = System.currentTimeMillis() + "";
        this.content = content;
    }

    public String getImageId() {
        return imageId;
    }

    public void setImageId(String imageId) {
        this.imageId = imageId;
    }

    public byte[] getContent() {
        return content;
    }

    public void setContent(byte[] content) {
        this.content = content;
    }
}

 2-controller-保存图片(前端fform表单上传图片即可)和读取图片(直接返回流)

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;


@RestController
@RequestMapping("/api/image/")
public class ApiImageCenterController{
    @Autowired
    private IImageCenterService iImageCenterService;

    /**
     *@描述 1-保存图片
     *@参数
     *@返回值
     *@创建人  TangYujie
     *@创建时间  2019/8/6 14:30
     */
    @RequestMapping(value="/save",method = RequestMethod.POST)
    public Object saveImage(
            @RequestParam(value = "file", required = true) MultipartFile file,
            HttpServletRequest request
    ) throws Exception {


        try {
            //保存图片
            byte[] pictureData = null;
            InputStream inputStream = file.getInputStream();
            pictureData = new byte[(int) file.getSize()];
            inputStream.read(pictureData);
            ImageCenter saveImag = new ImageCenter(pictureData,userId);
            iImageCenterService.insert(saveImag);//此步骤为保存图片;

            ///返回数据
            String key = "imageId";
            return ResponseMessageTypeEnum.CODE_SUCCESS.appendSingleDataToMap(key,saveImag.getImageId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseMessageTypeEnum.CODE_FAILURE.appendEmptyData();

        }
    }
    /**
     *@描述 2-查找图片
     *@参数
     *@返回值 直接返回流
     *@创建人  TangYujie
     *@创建时间  2019/8/8 15:26
     */
    @RequestMapping(value="/get/{imageId}",method=RequestMethod.GET)
    public void getPhotoById (@PathVariable("imageId")String imageId, final HttpServletResponse response) throws Exception{
        ImageCenter imageCenter = iImageCenterService.selectByPrimaryKey(imageId);
        byte[] data = imageCenter.getContent();
        response.setContentType("image/jpeg");
        response.setCharacterEncoding("UTF-8");
        OutputStream outputSream = response.getOutputStream();
        outputSream.write(data);
        outputSream.flush();
    }
}

3-mapper.xml

    <!-- 新增-->
    <insert id="insert" parameterType="ImageCenter" keyColumn="image_id"
            keyProperty="imageId">
        insert into s_image_center(
        "image_id",
        "content"
        ) values (
        #{imageId},
        #{content}
        )
    </insert> 

 

4-数据库设计

 

posted on 2019-10-25 18:09  我不吃番茄  阅读(1169)  评论(0编辑  收藏  举报