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

}

转载于:https://www.cnblogs.com/daohangtaiqian/p/6215648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值