package jp.dip.arimodoki.cntl;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import org.apache.commons.codec.binary.Base64;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import jp.dip.arimodoki.common.CConst;
@Scope("prototype")
@Controller
public class Base64Conv implements CConst {
@RequestMapping(value = "/base64input")
public String base64input() throws Exception {
return "base64";
}
@RequestMapping(value = "/base64convert")
public String base64convert(
@RequestParam MultipartFile uploadfile,
Model model
) throws Exception {
StringBuffer data = new StringBuffer();
InputStream is = uploadfile.getInputStream();
ByteArrayOutputStream os = new ByteArrayOutputStream();
byte[] indata = new byte[10240*16];
int siz;
while( ( siz = is.read(indata, 0, indata.length) ) > 0 ) {
os.write( indata, 0, siz );
}
String base64 = new String(Base64.encodeBase64(os.toByteArray()), "ASCII");
data.append("data:image/jpeg;base64,");
data.append(base64);
model.addAttribute("base64data", data.toString());
return "base64";
}
}
|