读书人

java 资料分割传输

发布时间: 2012-12-21 12:03:49 作者: rapoo

java 文件分割传输

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;elselastPieceSize = 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();}}}

读书人网 >编程

热点推荐