首先得到图片的Base64编码的字符串。转化成byte[]数组
String imgData = request.getParameter( "imgData");// Base64的字符串 BASE64Decoder decoder = new BASE64Decoder(); byte[] imgByte = decoder.decodeBuffer(imgData); for ( int i = 0; i < imgByte. length; ++i) { if (imgByte[i] < 0) { // 调整异常数据 imgByte[i] += 256; } }
/** * 获得20150803 --> 15/8/3,15/12/6,15/2/15,15/10/3文件夹形式 * * @param date * @return 15/10/3文件夹形式 */ public static String endFileDir () { Date date = new Date(System. currentTimeMillis()); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd" ); String str = sdf.format(date).toString(); StringBuffer sb = new StringBuffer(); char[] timeArr = str.toCharArray(); sb = sb.append(timeArr[2]).append(timeArr[3]); // str = ""+timeArr[2]+timeArr[3]; if (timeArr[4] != '0') { sb = sb.append(timeArr[4]); // str+=timeArr[4]; } sb = sb.append(timeArr[5]).append( "/"); // str+=""+timeArr[5]+"/";//根据当前系统环境确定分隔符 //确定天数作为文件夹,测试部不需要天数,直接注释即可 if(timeArr[6]!= '0'){ sb = sb.append(timeArr[6]); } sb = sb.append(timeArr[7]).append( "/"); return sb.toString().trim(); }
/** * @Title: uploadImg 上传的图片流 * @param path 图片上传的路径 * @param fileItem 图片文件 * @return * @return: boolean */ private static boolean uploadImg (String path, String imgName, byte[] imgByte) { // Linux服务器是反斜杠 path=path.replaceAll( "/", "\\\\"); File filePath = new File(path); filePath.setWritable( true, false); if (!filePath.exists()) { filePath.mkdirs(); } boolean isSuccess = false; File file = new File(path + imgName); FileOutputStream output = null; try { output = new FileOutputStream(file); output.write(imgByte); output.flush(); isSuccess = true; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); isSuccess = false; } finally { try { if (output != null) { output.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return isSuccess; }
public class FastUtil { private static StorageClient storageClient; private static TrackerServer trackerServer; public static final String FASTDFS_CONIFG="fdfs_client.conf" ;// Fastdfs的配置文件 static { try { // 初始化Fastdfs String path = new File(FastUtil.class.getResource("/" ).getFile()).getCanonicalPath(); String confPath = path + File. separator +FASTDFS_CONIFG; ClientGlobal. init(confPath); TrackerClient tracker = new TrackerClient(); trackerServer = tracker.getConnection(); StorageServer storageServer = null; storageClient = new StorageClient( trackerServer, storageServer); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } } /** * @Title: uploadImg * @Description:上传文件到fastdafs服务器 * @param fileBuff 文件的字节数组 * @param sExt 文件的后缀 * @return * @return: String 返回该文件的路径 */ public static String uploadImg (byte [] fileBuff, String sExt) { String[] result = null; try { result = storageClient.upload_file(fileBuff, sExt,null); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } if (result == null) { return null; } else { String imgPath="http://"+trackerServer .getInetSocketAddress().getHostName()+"/"+result[0]+ "/"+result[1]; return imgPath; } } }
/** * @Title: scale * @Description: 将图片缩放成固定的宽和高 * @param imgByte 缩放之后图片字节数组 * @param width 缩放之后的宽度 * @param height 高度 * @return * @return: byte[] 根据宽高缩放之后的图片 */ private static byte[] scale (byte [] imgByte, int w, int h) { byte[] newImgByte = null; try { Image img = ImageIO. read(new ByteArrayInputStream(imgByte)); // 根据原图与要求的缩略图比例,找到最合适的缩略图比例 int width = img.getWidth( null); int height = img.getHeight( null); if ((width * 1.0) / w < (height * 1.0) / h) { if (width > w) { h = Integer. parseInt(new java.text.DecimalFormat("0" ).format(height * w / (width * 1.0))); } } else { if (height > h) { w = Integer. parseInt(new java.text.DecimalFormat("0" ).format(width * h / (height * 1.0))); } } BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics g = bi.getGraphics(); g.drawImage(img, 0, 0, w, h, null, null); g.dispose(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); if (w == 640) { JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(byteArrayOutputStream); JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(bi); jep.setQuality(0.8f, true); encoder.encode(bi, jep); } else { ImageIO. write(bi, "PNG", byteArrayOutputStream); } newImgByte = byteArrayOutputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } return newImgByte; }