<< 2013年10月8日 | 首页 | 2013年10月10日 >>

cxf学习笔记之传递附件 - ll_feng - ITeye技术网站

注意事项 
1、服务端和客户端的数据对象中,用来存储附件的属性都要用“@XmlMimeType("application/octet-stream")”进行注解,如下: 
Java代码  收藏代码
  1. @XmlMimeType("application/octet-stream")  
  2. private DataHandler photo;  


2、服务端和客户端的web服务配置都要声明mtom-enabled=true 
服务端如果在spring中配置,如下: 
Java代码  收藏代码
  1. <jaxws:endpoint id="register"  implementor="cn.ibeans.ws.impl.RegisterWebServiceImpl" address="/ws/register">  
  2.     <jaxws:properties>  
  3.         <entry key="mtom-enabled" value="true"/>    
  4.     </jaxws:properties>  
  5. </jaxws:endpoint>  


客户端,如果用代码配置(也可用spring配置,类似服务端),如下: 
Java代码  收藏代码
  1. Map<String,Object> map = new HashMap<String,Object>();  
  2. map.put("mtom-enabled"true);  
  3. factory = new JaxWsProxyFactoryBean();  
  4. factory.setProperties(map);  

 

 

 

阅读全文……

标签 :

3 ways to read files using Java NIO | How to do in JAVA

New I/O, usually called NIO, is a collection of APIs that offer additional capabilities for intensive I/O operations. It was introduced with the Java 1.4 release by Sun Microsystems to complement an existing standard I/O. The extended NIO that offers further new file system APIs, called NIO2, was released with Java SE 7 (“Dolphin”).

NIO related questions are very popular in java interviews now-a-days.

NIO2 provides two major methods of reading a file:

  • Using buffer and channel classes
  • Using Path and Files classes

In this post, I am showing a couple of ways to read a file from file system. So lets start them by first showing old famous approach first, so that we can see what really changed.

Old famous I/O way

This example shows how we have been reading a text file using old I/O library APIs. It uses a BufferedReader object for reading. Another way can be using InputStream implementation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.howtodoinjava.test.nio;
 
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
 
public class WithoutNIOExample
{
    public static void main(String[] args)
    {
        BufferedReader br = null;
        String sCurrentLine = null;
        try
        {
            br = new BufferedReader(
            new FileReader("test.txt"));
            while ((sCurrentLine = br.readLine()) != null)
            {
                System.out.println(sCurrentLine);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (br != null)
                br.close();
            } catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
}

1) Read a small file in buffer of file size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.howtodoinjava.test.nio;
 
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
 
public class ReadFileWithFileSizeBuffer
{
    public static void main(String args[])
    {
        try
        {
            RandomAccessFile aFile = new RandomAccessFile(
                            "test.txt","r");
            FileChannel inChannel = aFile.getChannel();
            long fileSize = inChannel.size();
            ByteBuffer buffer = ByteBuffer.allocate((int) fileSize);
            inChannel.read(buffer);
            buffer.rewind();
            buffer.flip();
            for (int i = 0; i < fileSize; i++)
            {
                System.out.print((char) buffer.get());
            }
            inChannel.close();
            aFile.close();
        }
        catch (IOException exc)
        {
            System.out.println(exc);
            System.exit(1);
        }
    }
}

2) Read a large file in chunks with fixed size buffer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.howtodoinjava.test.nio;
 
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
 
public class ReadFileWithFixedSizeBuffer
{
    public static void main(String[] args) throws IOException
    {
        RandomAccessFile aFile = new RandomAccessFile
                ("test.txt", "r");
        FileChannel inChannel = aFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        while(inChannel.read(buffer) > 0)
        {
            buffer.flip();
            for (int i = 0; i < buffer.limit(); i++)
            {
                System.out.print((char) buffer.get());
            }
            buffer.clear(); // do something with the data and clear/compact it.
        }
        inChannel.close();
        aFile.close();
    }
}

3) Faster file copy with mapped byte buffer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package com.howtodoinjava.test.nio;
 
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
 
public class ReadFileWithMappedByteBuffer
{
    public static void main(String[] args) throws IOException
    {
        RandomAccessFile aFile = new RandomAccessFile
                ("test.txt", "r");
        FileChannel inChannel = aFile.getChannel();
        MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        buffer.load(); 
        for (int i = 0; i < buffer.limit(); i++)
        {
            System.out.print((char) buffer.get());
        }
        buffer.clear(); // do something with the data and clear/compact it.
        inChannel.close();
        aFile.close();
    }
}

All above techniques will read the content of file and print it to console. You can do whatever you want once you have read it.

阅读全文……

标签 :