How to store images to jackrabbit and deliver those images to HTML pages? Ex. If user will upload multiple images from one HTML page then he should be able to see all those images in next html page or any other page when required
This is the way:
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);
}
}
The 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();
}
}
来源:https://stackoverflow.com/questions/24394609/how-to-store-images-to-jackrabbit-and-deliver-those-images-to-html-pages