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