base64转文件

public static String decoderBase64File(String base64Code, String fileName) throws Exception, TradingException {
		
		if(fileName == null && "".equals(fileName))
			throw new TradingException("203/TradingException:fileName:" + fileName + " 必输项校验失败。");
		
		String fileId = null;
		BufferedInputStream bin = null;
		FileOutputStream fout = null;
		BufferedOutputStream bout = null;
		try {
			byte[] bytes = decoder.decodeBuffer(base64Code);
			ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
			bin = new BufferedInputStream(bais);
			
			// 指定目录上传到文件服务器
			String savePath = AppConfig.getProperty("Location_Default_Dir") + FileUtil.separator + "DZBH";
			File saveDir = new File(savePath);
			if (!saveDir.exists()) {
				saveDir.mkdir();
			}
			File file = new File(saveDir + File.separator + fileName);
						
			// 指定输出的文件
//			File file = new File("D:\\"+fileName);
						
			// 创建到指定文件的输出流
			fout = new FileOutputStream(file);
			// 为文件输出流对接缓冲输出流对象
			bout = new BufferedOutputStream(fout);

			byte[] buffers = new byte[1024];
			int len = bin.read(buffers);
			while (len != -1) {
				bout.write(buffers, 0, len);
				len = bin.read(buffers);
			}
			// 刷新此输出流并强制写出所有缓冲的输出字节,必须这行代码,否则有可能有问题
			bout.flush();
			// 文件上传
			fileId = FileUtil.uploadTempFile(2665l, file).getId();
			log.info("Base64保存文件成功");
			if(fileId == null || "".equals(fileId)){
				throw new TradingException("202/TradingException:保函接口附件上传失败 ");
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new TradingException(e.getMessage() + "/TradingException:" + getLogInfo(e));
		} finally {
			IOUtils.closeQuietly(bin);
			IOUtils.closeQuietly(fout);
			IOUtils.closeQuietly(bout);
		}
		return fileId;
	}

你可能感兴趣的:(JAVA)