zoukankan      html  css  js  c++  java
  • java io流 图片和字符串之间的转换

    package com.yundongsports.arena.controller.basketballsite;

    import com.yundongsports.arena.ResponseVo.Response;
    import org.apache.commons.io.IOUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.multipart.MultipartFile;
    import javax.servlet.http.HttpServletRequest;
    import java.io.*;
    import java.util.Date;
    import java.util.Map;
    import java.util.Properties;

    /**
    * Created by CF on 2016/12/23.
    */
    @Controller
    public class ImageUploadController {


    @Value("${base.web.url}")
    private String serverHost;
    /**
    * 将字符串转为图片
    * @param
    * @return
    */
    @RequestMapping(method = RequestMethod.POST, value = "/api/v1/imageUpload")
    @ResponseBody
    public Map<String,Object> generateImage(@RequestParam(value = "upload", required = false) MultipartFile uploadFile,
    HttpServletRequest req)throws Exception {
    Response response = new Response();
    System.out.println(uploadFile.getOriginalFilename()+"##"+serverHost);
    String imgFilePath = req.getServletContext().getRealPath("/") ;
    String img= "img/";

    String name=new Date().getTime()+uploadFile.getOriginalFilename();
    File file =new File(imgFilePath+img);
    //如果文件夹不存在则创建
    if (!file .exists() && !file .isDirectory()){
    file.mkdir();
    }
    InputStream input = null;
    OutputStream out = null;
    try {
    input = uploadFile.getInputStream();
    out = new FileOutputStream(imgFilePath+img+name);
    IOUtils.copy(input,out);
    out.flush();
    out.close();
    input.close();
    } catch (Exception e) {
    throw e;
    }finally{
    IOUtils.closeQuietly(input);
    IOUtils.closeQuietly(out);
    }
    String url=serverHost+img+name;
    System.out.println("服务器的绝对路径-------------------"+url);
    response.put("data",url);
    response.setResult(Response.TYPE.SUCCESS);
    return response.getResult();
    }

    }
  • 相关阅读:
    asyncio异步IO--协程(Coroutine)与任务(Task)详解
    python爬虫实战:利用scrapy,短短50行代码下载整站短视频
    深入理解Git的实现原理
    Upsource 代码审查工具安装及使用
    MAC MAMP集成环境安装 PHP 扩展
    千万数据量数据表分表实践
    设计模式:序言
    设计模式 行为型
    PHP5底层原理之变量
    PHP5底层原理之垃圾回收机制
  • 原文地址:https://www.cnblogs.com/daohangtaiqian/p/6215648.html
Copyright © 2011-2022 走看看