How to convert an InputStream to a DataHandler?

后端 未结 7 752
挽巷
挽巷 2020-12-05 07:20

I\'m working on a java web application in which files will be stored in a database. Originally we retrieved files already in the DB by simply calling getBytes o

7条回答
  •  庸人自扰
    2020-12-05 07:39

    I've meet situation, when InputStream requested from DataSource twice: using Logging Handler together with MTOM feature. With this proxy stream solution my implementation works fine:

    import org.apache.commons.io.input.CloseShieldInputStream;
    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    ...
    
    private static class InputStreamDataSource implements DataSource {
        private InputStream inputStream;
    
        @Override
        public InputStream getInputStream() throws IOException {
            return new CloseShieldInputStream(inputStream);
        }
    
        @Override
        public OutputStream getOutputStream() throws IOException {
            throw new UnsupportedOperationException("Not implemented");
        }
    
        @Override
        public String getContentType() {
            return "application/octet-stream";
        }
    
        @Override
        public String getName() {
            return "";
        }
    }
    

提交回复
热议问题