二进制内容由xml中提交过来的,需要把它转成文件保存到指定目录中。
二进制内容:FFD8FFE000104A46494600010100000100010000FFDB0043000503040404030504040405050506070C08070707070F.....
先将其hex解码然后写输出流
[code="java"]
/*
import static java.lang.System.out;
/**
@since 2009-06-05
/
public final class TestHex
{
/*
/**
@return corresponding unsigned byte array. see http://mindprod.com/jgloss/unsigned.html
*/
public static byte[] fromHexString( String s )
{
int stringLength = s.length();
if ( ( stringLength & 0x1 ) != 0 )
{
throw new IllegalArgumentException( "fromHexString requires an even number of hex characters" );
}
byte[] bytes = new byte[stringLength / 2];
for ( int i = 0, j = 0; i < stringLength; i += 2, j++ )
{
int high = charToNibble( s.charAt( i ) );
int low = charToNibble( s.charAt( i + 1 ) );
// You can store either unsigned 0..255 or signed -128..127 bytes in a byte type.
bytes[ j ] = ( byte ) ( ( high << 4 ) | low );
}
return bytes;
}
// -------------------------- STATIC METHODS --------------------------
static
{
// only 0..9 A..F a..f have meaning. rest are errors.
for ( int i = 0; i <= 'f'; i++ )
{
correspondingNibble[ i ] = -1;
}
for ( int i = '0'; i <= '9'; i++ )
{
correspondingNibble[ i ] = ( byte ) ( i - '0' );
}
for ( int i = 'A'; i <= 'F'; i++ )
{
correspondingNibble[ i ] = ( byte ) ( i - 'A' + 10 );
}
for ( int i = 'a'; i <= 'f'; i++ )
{
correspondingNibble[ i ] = ( byte ) ( i - 'a' + 10 );
}
}
/**
* convert a single char to corresponding nibble using a precalculated array.
* Based on code by:
* Brian Marquis
* Orion Group Software Engineers http://www.ogse.com
*
* @param c char to convert. must be 0-9 a-f A-F, no
* spaces, plus or minus signs.
*
* @return corresponding integer 0..15
* @throws IllegalArgumentException on invalid c.
*/
private static int charToNibble( char c )
{
if ( c > 'f' )
{
throw new IllegalArgumentException( "Invalid hex character: " + c );
}
int nibble = correspondingNibble[ c ];
if ( nibble < 0 )
{
throw new IllegalArgumentException( "Invalid hex character: " + c );
}
return nibble;
}
/**
* code not used, for explanation only.
* convert a single char to corresponding nibble.
* Slow version, easier to understand.
*
* @param c char to convert. must be 0-9 a-f A-F, no
* spaces, plus or minus signs.
*
* @return corresponding integer
*/
private static int slowCharToNibble( char c )
{
if ( '0' <= c && c <= '9' )
{
return c - '0';
}
else if ( 'a' <= c && c <= 'f' )
{
return c - 'a' + 0xa;
}
else if ( 'A' <= c && c <= 'F' )
{
return c - 'A' + 0xa;
}
else
{
throw new IllegalArgumentException( "Invalid hex character: " + c );
}
}
// --------------------------- main() method ---------------------------
/**
* Test harness
*
* @param args not used
*/
public static void main( String[] args )
{
String hexString = "FFD8FFE000104A46494600010100000100010000FFDB0043000503040404030504040405050506070C08070707070F";
out.println( hexString );
// convert hex string to an array of bytes
byte[] bytes = fromHexString( hexString );
out.println( bytes.length + " bytes long" );
try{
OutputStream os=new FileOutputStream("d:\a.data");
os.write(bytes);
os.flush();
os.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
}
[/code]
保存到指定目录中新建文件
吧读到的内容 写入 :wink: