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

    }
  • 相关阅读:
    下拉复选框
    tp mysql 去重
    前端面试准备2----Javascript中的Undefined和null小结
    前端面试准备1----JS中eval()解析和为什么不要使用eval
    点击一个按钮触发文件选择
    解决JS在url中传递参数时参数包含中文乱码的问题
    asp.net文件/大文件上传需要配置的项目整理
    网页元素位置、鼠标事件位置信息小结
    DOM事件总结
    学习require.js中的一些总结
  • 原文地址:https://www.cnblogs.com/daohangtaiqian/p/6215648.html
Copyright © 2011-2022 走看看