如何将图像存储到长兔并将这些图像传送到HTML页面?

时间:2014-06-24 19:21:09

标签: java jackrabbit jcr

如何将图像存储到长兔并将这些图像传送到HTML页面? 防爆。如果用户将从一个HTML页面上传多个图像,那么他应该能够在下一个html页面或任何其他页面中查看所有这些图像

1 个答案:

答案 0 :(得分:3)

这是方式:

 public class JackRabbitServiceImpl {

        Repository repository = new TransientRepository(); 

        public JackRabbitServiceImpl() throws Exception{
            Session session = repository.login( 
                    new SimpleCredentials("username", "password".toCharArray()));
            try{
                InputStream stream = new BufferedInputStream(JackRabbitServiceImpl.class.getResourceAsStream("red_rose.jpg"));
                Node folder = session.getRootNode(); 
                Node file = folder.addNode("redrose.jpg","nt:file");
                Node content = file.addNode("jcr:content","nt:resource");
                Binary binary = session.getValueFactory().createBinary(stream);
                content.setProperty("jcr:data",binary);
                content.setProperty("jcr:mimeType","image/gif");
                session.save(); 
            }finally{
                session.logout(); 
            }
        }

        public byte[] getContentRose() throws Exception{
            Session session = repository.login( 
                    new SimpleCredentials("username", "password".toCharArray()));
            Node folder = session.getRootNode(); 
            Node file=folder.getNode("redrose.jpg");
            Node content=file.getNode("jcr:content");
            String path = content.getPath();
            Binary bin = session.getNode(path).getProperty("jcr:data").getBinary();
            InputStream stream = bin.getStream();
            return IOUtils.toByteArray(stream);
        }

    }

servlet

public class TestJackRabbitCdn extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 4158924623219324725L;
    JackRabbitServiceImpl service=null;
    @Override
    public void init(){
        try {
            service=new JackRabbitServiceImpl();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,
    ServletException {
        response.setContentType("image/gif");
        try {
            response.getOutputStream().write(service.getContentRose());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        response.getOutputStream().flush();
    }

}