1、直接获取文件,转based4
/**
* 2017-7-17
* @param id
* @return
*/
@RequestMapping("add")
public void add(CqscBrayData cqscBrayData,MultipartHttpServletRequest request,
HttpServletResponse response){
response.setContentType("text/html; charset=utf-8");
MultipartFile fileCrl = request.getFile("myUploadCrl");
CommonsMultipartFile cf= (CommonsMultipartFile)fileCrl;
DiskFileItem fi = (DiskFileItem)cf.getFileItem();
File f = fi.getStoreLocation();
if(fi.getSize() != 0){
byte[] bs = null;
try {
bs = FileUtils.readFileToByteArray(f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 加密为base64编码字符串
BASE64Encoder encoder = new BASE64Encoder();
String bsCrl = encoder.encode(bs);
System.out.println(bsCrl);
cqscBrayData.setCrlBase64(bsCrl);
}
try {
om.writeValue(response.getWriter(), map);
response.getWriter().flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
response.getWriter().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、从本地获取文件,转based4
public static String getBase64Path(String img_path){
String photopath = PropertiesUtil.getProp("photoPath");
String replac = photopath.substring(3);
String path = photopath.replace(replac, img_path);
File file = new File(path);
byte[] bs = null;
if(file.exists()){
try {
bs = FileUtils.readFileToByteArray(file);
// 加密为base64编码字符串
BASE64Encoder encoder = new BASE64Encoder();
String baseStr = encoder.encode(bs);
System.out.println(baseStr);
} catch (IOException e1) {
e1.printStackTrace();
}
}
return path;
}