I have a JTextPane and I do it :
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();`
this.setEditorKit(kit);
this.setDocument(doc);
Then I do :
profilePictureSrc = "http://ola/profilePicture1.jpg";
chatContent ="<img src=\"" + profilePictureSrc + "\">";
Where profilePictureSrc is a URL Object.
It works but I must use a String instead of the URL (Java Hashtable put method slow down my application)
How Can I do that ? Do I have to put the picture files somewhere and use a relative Path to reach them ? Thank you very much for your ideas
Best Regards
You can convert url objects to strings.
String urlstring = myurl.toString();
Related
I've got a program outputs some URLs to a JEditorPane. I want the URLs to be hyperlinks. The program will basically output the URLS to the JEditorPane as if it were a log.
I've got it somewhat working, but it's not hyperlinking the URLs.
Here's the code I have:
JEditorPane editorPane = new JEditorPane();
editorPane.setEditorKit(JEditorPane.createEditorKitForContentType("text/html"));
editorPane.setEditable(false);
editorPane.addHyperlinkListener(new HyperlinkListener() {
//listener code here
});
//some other code here
StyledDocument document = (StyledDocument) editorPane.getDocument();
String url = "http://some url";
String newUrl = "\n"+url+"\n";
document.insertString(document.getLength(), "\n" + newUrl + "\n", null);
Instead of http://example.com/ it's outputting:
http://example.com/
If I don't use a StyledDocument and just do editorPane.setText(newUrl) it does correctly hyperlink the URLs, but it has the obvious problem that setText will replace whatever was already there.
When you use editorPane.setText(), the method will use the editor kit to insert the string. That means it will analyze it, style it and then use document.insertString() with the appropriate styles to create the expected effect.
If you call document.insertString() directly, you're circumventing the editor kit -> no styling. Have a look at the source code for setText() to see how it's done: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/javax/swing/JEditorPane.java#JEditorPane.setText%28java.lang.String%29
Because of copyright, I can't copy the code here. This should get you started:
Document doc = editorPane.getDocument();
EditorKit kit = editorPane.getEditorKit();
StringReader r = new StringReader(newUrl);
kit.read(r, doc, doc.getLength());
sorry if it's kind of a big question but I'm just looking for someone to tell me in what direction to learn more since I have no clue, I have very basic knowledge of HTML and Java.
Someone in my family has to copy every product from a supplier into his own webshop.
The problem is he needs to put in all the articles one by one by hand,I'm looking for a way to replace him by a program.
I already got a bit going on for the price calculation , all I need now is the info of the product.
http://pastebin.com/WVCy55Dj
From line 1009 to around 1030.
I need 3 seperate strings of the three span's with the class "CatalogusListDetailTest"
From line 987 to around 1000.
I need a way to get all these images, it's on the website at www.flamingo.be/Images/Products/Large/"productID"(our first string).jpg
sometimes there's a _A , _B as you can see in this example so I'm looking for a way to make it check if there is and get these images aswell.
If I could get this far then I'd be very thankful ! I'll figure the rest out myself, sorry for the long post, wanted to give as much info as possible.
You can look at HTML parser lib Jsoup, doc reference: http://jsoup.org/cookbook/
EDIT: Code to get the product code:
Elements classElements = document.getElementsByClass("CatalogusListDetailTextTitel");
for (Element classElement : classElements) {
if (classElement.text().contains("Productcode :")) {
System.out.println(classElement.parent().ownText());
}
}
Instead of document you may have to use an element to get the consistent result, above code will print all the product codes.
You can use JTidy for what you need.
Code Example:
public void downloadSinglePage(String pageLink, String targetDir) throws XPathExpressionException, IOException {
URL url = new URL(pageLink);
BufferedInputStream page = new BufferedInputStream(url.openStream());
Tidy tidy = new Tidy();
tidy.setQuiet(true);
tidy.setShowWarnings(false);
Document response = tidy.parseDOM(page, null);
XPathFactory factory = XPathFactory.newInstance();
XPath xPath=factory.newXPath();
NodeList nodes = (NodeList)xPath.evaluate(IMAGE_PATTERN, response, XPathConstants.NODESET);
String imageURL = (String) nodes.item(0).getNodeValue();
saveImageNIO(imageURL, targetDir);
}
where
IMAGE_PATTERN = "///a/img/#src";
but the pattern depends on how the image is innested in the page HTML code.
Method for saving Image using NIO:
public void saveImageNIO(String imageURL, String targetDir, String imageName) throws IOException {
URL url = new URL(imageURL);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(targetDir + "/" + imageName + ".jpg");
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
}
I'm creating a java program that will read a html document from a URL and display the sizes of the images in the code. I'm not sure how to go about achieving this though.
I wouldn't need to actually download and save the images, i just need the sizes and the order in which they appear on the webpage.
for example:
a webpage has 3 images
<img src="dog.jpg" /> //which is 54kb
<img src="cat.jpg" /> //which is 75kb
<img src="horse.jpg"/> //which is 80kb
i would need the output of my java program to display
54kb
75kb
80kb
Any ideas where i should start?
p.s I'm a bit of a java newbie
If you're new to Java you may want to leverage an existing library to make things a bit easier. Jsoup allows you to fetch an HTML page and extract elements using CSS-style selectors.
This is just a quick and very dirty example but I think it will show how easy Jsoup can make such a task. Please note that error handling and response-code handling was omitted, I merely wanted to pass on the general idea:
Document doc = Jsoup.connect("http://stackoverflow.com/questions/14541740/java-program-to-download-images-from-a-website-and-display-the-file-sizes").get();
Elements imgElements = doc.select("img[src]");
Map<String, String> fileSizeMap = new HashMap<String, String>();
for(Element imgElement : imgElements){
String imgUrlString = imgElement.attr("abs:src");
URL imgURL = new URL(imgUrlString);
HttpURLConnection httpConnection = (HttpURLConnection) imgURL.openConnection();
String contentLengthString = httpConnection.getHeaderField("Content-Length");
if(contentLengthString == null)
contentLengthString = "Unknown";
fileSizeMap.put(imgUrlString, contentLengthString);
}
for(Map.Entry<String, String> mapEntry : fileSizeMap.entrySet()){
String imgFileName = mapEntry.getKey();
System.out.println(imgFileName + " ---> " + mapEntry.getValue() + " bytes");
}
You might also consider looking at Apache HttpClient. I find it generally preferable over the raw URLConnection/HttpURLConnection approach.
You should break you problem into 3 sub problems
Download the HTML document
Parse the HTML document and find the images
Download the images and determine its size
You can use regular expressions to find tag and get image URL. After that you'll need and HttpUrlConnection class to get image data and measure it's size.
You can do this:
try {
URL urlConn = new URL("http://yoururl.com/cat.jpg");
URLConnection urlC = urlConn.openConnection();
System.out.println(urlC.getContentLength());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This question has been asked few times in forums, but in my code, i can't display my image. I think it's not the right method :
webViewContact.loadData(db.getParametres().get(0).getInformationParam(), "text/html", "utf-8");
getInformationParam() recup the HTML code, like :
<img src=\\"file:///android_asset/logoirdes_apropos.jpg\\"/> <b>Test</b>
My image file is in drawable, how i can display it ?
There are restrictions about the HTML loaded with loadData() can do. Suggest using loadUrl:
webViewContact.loadUrl("file:///android_asset/" + db.getParametres().get(0).getInformationParam())
You can try the following code, and your file will be at: htmlFile. You can certainly do it in UI thread for now, but you might consider to move this to a AsyncTask later in real production if the file is huge.
String directory = Environment.getExternalStoragePublicDirectory("html_cache");
Writer output;
try {
directory.mkdir();
File htmlFile = new File(directory + File.separator + "give_a_name.html");
String content = db.getParametres().get(0).getInformationParam();
// assumes default encoding is OK!
output = new BufferedWriter(new FileWriter(htmlFile));
output.write( aContents );
}
finally {
output.close();
}
I am using a JTextPane to display data from a webpage that isn't mine, so I have no control over its contents. It requires a user to be logged in, so I use URLConnections to connect to that webpage and use cookies in the URLConnection to retrieve data. That works fine. However, when I put this data in a JTextPane with the content type set to text/html, the images do not display as they require those cookies with the session id and stuff to be sent in order to retrieve the uploaded images.
Is there any way I can make the JTextPane (though I am able to use anything else in the jdk that displays html) use my cookies?
Thanks.
I store the cookies in a linked list:
loadText = "Logging in...";
url = new URL("http://www.example.com/login.php");
connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(
connection.getOutputStream());
out.write("username=" + URLEncoder.encode(username, "UTF-8")
+ "&password=" + URLEncoder.encode(password, "UTF-8")
+ "&testcookies=1");
out.flush();
out.close();
List<String> cookies = new LinkedList<String>();
for (int i = 1; (headerName = connection.getHeaderFieldKey(i)) != null; i++) {
if (headerName.equals("Set-Cookie")) {
String cookie = connection.getHeaderField(i);
cookie = cookie.substring(0, cookie.indexOf(";"));
cookies.add(cookie);
}
}
And I also need to strip away unneccesary HTML, which gives me a string I plug into the textpane:
String p1 = rawPage.split("<div id=\"contentstart\">")[1]
.split("</div><!--id='contentstart'-->")[0];
p1 = p1.replaceAll("<p><strong></strong></p>", "");
p1 = p1.replaceAll("<p></p>", "");
parsed = true;
JTextPane tp = new JTextPane();
tp.setEditable(false);
JScrollPane js = new JScrollPane();
js.getViewport().add(tp);
js.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
getContentPane().add(js);
js.setSize(640, 480);
tp.setContentType("text/html");
tp.setText(p1);
Are you not reading the content from URLConnection? Something like this may help.
Post your code so that we can get more insight.
JTextPane pane;
..
HTMLDocument htmlDocument = (HTMLDocument) pane.getDocument();
htmlDocument.putProperty("IgnoreCharsetDirective", Boolean.TRUE);
htmlDocument.putProperty(Document.StreamDescriptionProperty, pageUrl);
pane.read(connection.getInputStream, htmlDocument);
-- or --
You may try the browser swing component instead of JTextPane.
http://djproject.sourceforge.net/ns/index.html
Cookies are stored in relation to your browser. For example, if you have some cookies in Firefox, Microsoft IE can't see those cookies. Similarly, the cookies you have obtained from the webpage you're looking for are not available to your Java application.
But also, JTextPane is not a full-featured HTML browser. You can use it to render basic HTML (actually HTML 2.0, a much older version of HTML), but it won't work with cookies, CSS, and other now-standard web features.
You may want to look at full-featured web browsers, such as Flying Saucer - see http://weblogs.java.net/blog/2007/07/14/flying-saucer-r7-out
But even if you do this, Flying Saucer won't see the cookies that you've obtained through other browsers.