首页>代码>基于html5图片上传,支持缩放和图片质量修改>/imgUpload/src/main/java/com/saic/frontweb/controller/ImgUploadController.java
package com.saic.frontweb.controller;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.saic.ebiz.mediastorage.exception.MediaStorageFailException;
import com.saic.ebiz.mediastorage.service.MediaStorageService;
@Controller
@RequestMapping("/imgupload")
public class ImgUploadController {
@Autowired
private MediaStorageService mediaStorageService;
@RequestMapping("/show")
public ModelAndView showUpload(){
String html = "/index.html";
ModelAndView model = new ModelAndView(html);
return model;
}
@RequestMapping("/do")
@ResponseBody
public String doUpload(@RequestParam(value="base64",required=true) String imgBase64,@RequestParam(value="type",required=true) String type) throws IOException{
System.out.println("type=="+type);
//获取真正的base64编码
String img = imgBase64.substring(imgBase64.indexOf("base64")+7);
Base64 base64 = new Base64();
ByteArrayInputStream in = new ByteArrayInputStream(base64.decode(img));
String imgSrc = null;
Map<String,String> resMap = new HashMap<String,String>();
resMap.put("base64", imgBase64);
try {
imgSrc = mediaStorageService.storageMedia(in,type.substring(type.indexOf("/")+1));
resMap.put("imgSrc", imgSrc);
} catch (MediaStorageFailException e) {
System.out.println("图片异常");
}
return imgSrc;
}
public static void main(String[] args) throws IOException{
File file = new File("D://百度云//照片//20141103//IMG_20140926_205437.jpg");
FileInputStream inputFile = new FileInputStream(file);
byte[] buffer = new byte[(int) file.length()];
inputFile.read(buffer);
inputFile.close();
Base64 base64 = new Base64();
System.out.println(base64.encodeToString(buffer));
}
}
最近下载更多

最近浏览