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

java io流 图片和字符串之间的转换的更多相关文章

  1. Java IO流详解(六)——转换流

    转换流也是一种处理流,它提供了字节流和字符流之间的转换.在Java IO流中提供了两个转换流:InputStreamReader 和 OutputStreamWriter,这两个类都属于字符流.其中I ...

  2. <摘录>Gson对Java嵌套对象和JSON字符串之间的转换

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,具有良好的跨平台特性.近几年来已经和XML一样成为C/S架构中广泛采用的数据格式.有关JSON的更多知识, ...

  3. java中字符数组与字符串之间互相转换的方法

    public static void main(String[] args) { //1.字符数组 转换成 字符串 //(1)直接在构造String时转换 char[] array = new cha ...

  4. java IO流读取图片供前台显示

    最近项目中需要用到IO流来读取图片以提供前台页面展示,由于以前一直是用url路径的方式进行图片展示,一听说要项目要用IO流读取图片感觉好复杂一样,但任务下达下来了,做为程序员只有选择去执行喽,于是找了 ...

  5. java IO流详解

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  6. Java IO流学习总结

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  7. Java IO流详尽解析

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  8. JAVA.IO流学习笔记

    一.java.io 的描述 通过数据流.序列化和文件系统提供系统输入和输出.IO流用来处理设备之间的数据传输 二.流 流是一个很形象的概念,当程序需要读取数据的时候,就会开启一个通向数据源的流,这个数 ...

  9. 【转载】JAVA IO 流的总结

    来自http://www.cnblogs.com/oubo/archive/2012/01/06/2394638.html,写的很详细 Java流操作有关的类或接口: Java流类图结构: 流的概念和 ...

随机推荐

  1. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  2. Linux2.6.11版本:classic RCU的实现

    转载自:http://www.wowotech.net/kernel_synchronization/linux2-6-11-RCU.html 一.前言 无论你愿意或者不愿意,linux kernel ...

  3. Max double slice sum 的解法

    1. 上题目: Task description A non-empty zero-indexed array A consisting of N integers is given. A tripl ...

  4. linux -目录结构

    摘自:http://www.comptechdoc.org/os/linux/usersguide/linux_ugfilestruct.html 这个目录结构介绍是我目前看到介绍最全的,有时间在翻译 ...

  5. bzoj1492 斜率优化|cdq分治

    #include <stdio.h> #include <bitset> #include <string.h> #include <stack> #i ...

  6. BZOJ3522——[Poi2014]Hotel

    1.题意:http://www.lydsy.com/JudgeOnline/problem.php?id=3522 2.分析:这道题有两种情况,第一个是三个点在同一个链上,这显然不可能了,因为树上的路 ...

  7. WinForm各种API---时时更新

    好文要顶 关注我 收藏该文 徐淳 关注 - 1 粉丝 - 3       0 0     本文原文地址:http://www.cnblogs.com/hqxc/p/6160685.html 徐淳 [D ...

  8. markdown编辑器sublime text3

    安装包管理Package Control ctrl+`或者点击查看-->显示面板,复制下面的代码到面板里. import urllib.request,os; pf = 'Package Con ...

  9. 一次erlang 节点CPU严重波动排查

    新服务上线后观察到,CPU在10 ~ 70%间波动严重,但从每秒业务计数器看业务处理速度很平均. 接下来是排查步骤: 1. dstat -tam 大概每10s一个周期,网络流量开始变得很小,随后突然增 ...

  10. js 数组,字符串,json互相转换

    数组转字符串 var arr = [1,2,3,4,'巴德','merge']; var str = arr.join(','); console.log(str); // 1,2,3,4,巴德,me ...