详细的代码过程参考网上:
//跨越的设置
Map <String,String> result = new HashMap<String,String>();
response.setHeader("Access-Control-Allow-Origin", "*");//跨域访问··
String imgFilePath="";//写入文件的目录
String dbpath="";//写入数据库中路径,返回可以直接访问的
//项目路径
String tcatpath = request.getServletContext().getRealPath("/");
System.out.println("输出的项目路径"+tcatpath);
//随机数命名
Random rand = new Random();
String picName= ""+(rand.nextInt(900000)+100000);//生成6位的随机数
//上传图片 到项目中的某一个文件夹中,并且对base64进行解码
String img=request.getParameter("userPic");//接受图片
System.out.println("测试从移动端的测试的获取的base64的 字符串");
// String img=imgStr.replace(" ", "+");//base64解码中注意“+”和空格的只爱电脑看到你管
BASE64Decoder decoder = new BASE64Decoder();
System.out.println("开始解码");
try
{
//Base64解码
byte[] b = decoder.decodeBuffer(img);
//添加到服务器的地址:http://liuwu.site/
imgFilePath = tcatpath+"\\imgUserHeard\\"+picName+".jpg";//
//拼出字符串;存入数据库中。
dbpath="http://mishuixunhuanx.com/msxh-web-api/imgUserHeard/"+picName+".jpg";
System.out.println("输出存储的路径"+imgFilePath);
OutputStream out = new FileOutputStream(imgFilePath);
logger.info("生成图片");
out.write(b);
out.flush();
out.close();
}
catch (Exception e)
{
logger.info("解码失败");
}
/*****
* 图书上传成功。
* 插入地址,同时返回一个地址
*/