java 文件分割传输

public class Base64FilterOutputStream  extends FilterOutputStream{

	public Base64FilterOutputStream(OutputStream out) {
		super(out);
		
	}
	@Override
	public void write(int b)throws IOException{
		if(b!=13&&b!=32&&b!=10)
			super.write(b);
	}
}

 

public static File convert2Base64File(InputStream is) throws IOException{
		BASE64Encoder b64=new BASE64Encoder();
		Random rd=new Random();
		File file=new File(System.getProperty("user.dir")+File.separator+rd.nextInt(99999));
		OutputStream bos=new Base64FilterOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
		b64.encodeBuffer(is, bos);
		bos.flush();
		bos.close();
		is.close();
		return file;
	}

 

private static final int FILE_PIECE_SIZE = 1024 * 100;
 

 

public void upgradeVersion(BmVersionUpgrade pkg, InputStream is) throws UserOperationError {
		File f = null;
		try {
			f = convert2Base64File(is);
			long size = f.length();
			long lastPieceSize = size % FILE_PIECE_SIZE;
			long count = size / FILE_PIECE_SIZE;
			if (lastPieceSize > 0)
				count += 1;
			else
				lastPieceSize = FILE_PIECE_SIZE;

			
			Reader reader = IOUtils.getReader(f, "US-ASCII");
			
			char[] b = new char[FILE_PIECE_SIZE];
			for (int i = 0; i < count; i++) {
				int len = reader.read(b);
				String content = new String(b, 0, len);
                                //将content放入xml中
                                ByteArrayOutputStream out=new ByteArrayOutputStream();
                                TransformerFactory tf = TransformerFactory.newInstance();
		                Transformer t = null;
		                try {
			             t = tf.newTransformer();
			            if(wrapLine)
				         t.setOutputProperty(OutputKeys.INDENT, "yes");
			                 t.setOutputProperty(OutputKeys.METHOD, "xml");
			                 t.setOutputProperty(OutputKeys.ENCODING, "utf-8");
		                 } catch (TransformerConfigurationException tce) {
			
		                 }
		                 DOMSource doms = new DOMSource(doc);
		                 StreamResult sr = new StreamResult(os);
		                 try {
			             t.transform(doms, sr);
		                 } catch (TransformerException te) {
			            //处理异常
		                  }
                                  out.flush();
		                  out.close();
                                 byte[] bs = out.toByteArray();
                                 //TODO bs可以在网络上传输
			}
			f.delete();
		} catch (IOException e) {
			e.printStackTrace();
			throw new UserOperationError(e.getMessage());
		}  finally {
			if(f != null) {
				f.delete();
			}
		}
	}

你可能感兴趣的:(xml,F#,OS)