CommonsMultipartFile 为spring上传文件特有的属性 ,从别人博客找到了文件转 base64 的方法,代码如下:
/** * 文件转base64字符串 * @param file * @return */ public static String fileToBase64(File file) { String base64 = null; InputStream in = null; try { in = new FileInputStream(file); byte[] bytes = new byte[in.available()]; int length = in.read(bytes); base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return base64; } /** * base64字符串转文件 * @param base64 * @return */ public static File base64ToFile(String base64) { File file = null