The problem my application reads in jpegs and displays them on a jlabel (these are pictures of books)
Everything works fine when used with the local version e.g. reading from the C drive, but once I try to do this over the internet problems occur that I have tried without success to correct
Scenario
Should the jpeg not be present at the end of the url I get the following error
javax.imageio.IIOException: Can't get input stream from URL!
In the version that reads from the local drive I detect if the file exist and overcome this problem however I have tried lots of the ideas posted and I simply can’t find out how to detect that the jpeg is absent!
Please can some one help
here are the two version of the code
Read from local drive C
private void showcover() {
String stockPic;
String partofISBN;
String completeurl;
jButton9.setVisible(true);
stockPic = jTextField1.getText();// get the current isbn
partofISBN = stockPic.substring(0, 7); // get first 7 numbers
String picUrl;
stockPic = stockPic + localNumber + ".jpg";
picUrl = partofISBN + "\\" + stockPic;
completeurl = "C:\\Apicture\\" + picUrl;
File pf = new File(completeurl);
if (!pf.exists()) {
jLabel9.setIcon(new ImageIcon("C:\\Apicture\\" + picUrl));
jLabel9.setIcon(new ImageIcon("C:\\Apicture\\nojpegs.jpg"));
jLabel9.setText("NO Jpeg");
}
jLabel9.setIcon(new ImageIcon(completeurl));
}
Adaption to read from url
URL url;
url = new URL("http://ebid.s3.amazonaws.com/upload_big/9/1/1/1401018425-17770-385.jpg");
Image image = null;
try {
image = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
javax.imageio.IIOException means your are not getting the image.
So add more code to failover to alternate URL/disk in your catch(IOException ) block.
Read http HEAD request and response code 404 for not found. Http is your friend.
Find out of the resource exists before you GET it.
You may want to check this answer I just provided: I can't download a specific image using java code
Seems like amazon is also checking your user-agent, so you may have to put something like this at the beginning of your code:
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
edit: "at the beginning of your code" really means something like "before creating any URL-related object". I mean, I didn't want to refer to the code you posted, but you whole application.
Use this:
URL url;
url = new URL("http://ebid.s3.amazonaws.com/upload_big/9/1/1/1401018425-17770-385.jpg");
url.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
Image image = null;
try {
image = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
Related
Hey all trying to write a web scraper that downloads all the songs from this website
https://billwurtz.com/songs.html
but some of his older URLs contain spaces ("%20") such as https://billwurtz.com/can%20i.mp3. Which on his newer links he has changed to '-'.
Anyway, when I try to download these tracks I get a 400 Error (Bad Request Error) which makes me think that java is sending the request with and not %20
URL website = new URL("https://www.billwurtz.com/" + song);
Path out = Path.of("songs/" + song);
System.out.println(website.toString());
try (InputStream in = website.openStream()) {
Files.copy(in, out, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
// missed.add(song + ":" + e.getMessage());
throw new Exception(e.getMessage());
}
returns
https://www.billwurtz.com/can%20i.mp3
Exception in thread "main" java.lang.Exception: Server returned HTTP response code: 400 for URL: https://billwurtz.com/can i.mp3
I tried looking about but only came across issues created from the programmer trying to create a URL that contains (spaces) but here it is clear that it is using %20
Thank you all for your time :)
For bilingual support in an application I am working on, we are using Spring messaging which uses two files, ApplicationResources.properties and ApplicationResources_fr.properties. This works well.
Now I am trying to expand on this by making it a little more dynamic. The application will read key value pairs from the database and insert them, which gives me the following error:
java.io.FileNotFoundException: \ApplicationResources.properties (Access is denied)
I am able to check on the key value pairs so I know the path I am using is correct. I have also checked the files in Eclipse properties by right clicking, and by visiting the actual file on my system, and they are not read-only. I do not believe they are encrypted because I am able to open and view with notepad++.
Here is my testing code which shows I can view them
Properties test_prop = null;
InputStream is = null;
try {
test_prop = new Properties();
is = this.getClass().getResourceAsStream(en_path);
test_prop.load(is);
Set<Object> keys = test_prop.keySet();
boolean key_found = false;
for(Object k:keys) {
String key = (String)k;
if(key.equals("f12345"))
{
key_found=true;
break;
}
}
System.out.println("Language Properties Test in DAO:" + (key_found? "Key Found" : "Key not found"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Here is where I try to write to the file, and get the error:
ResultSet rs = null;
try (
Connection connection = jdbcTemplate.getDataSource().getConnection();
CallableStatement callableStatement = connection.prepareCall(test_prod_cur);
)
{
callableStatement.registerOutParameter(1, OracleTypes.CURSOR);
callableStatement.executeUpdate();
rs = (ResultSet) callableStatement.getObject(1);
while (rs.next())
{
String thead = rs.getString(1);
//System.out.println(thead + " " + rs.getString(2) + " " + rs.getString(3));
en_prop.setProperty(keyheader+thead, rs.getString(2));
fr_prop.setProperty(keyheader+thead, rs.getString(3));
}
}
catch (SQLException e)
{
System.out.println("SQLException - bilingual values - CLUDAOImpl");
System.out.println(e.getMessage());
}
//add to properties files
//*
try (OutputStream en_os = new FileOutputStream(en_path);)
{
en_prop.store(en_os, null);
} catch (IOException e) {
e.printStackTrace();
}
try(OutputStream fr_os = new FileOutputStream(en_path);)
{
fr_prop.store(fr_os, null);
} catch (IOException e) {
e.printStackTrace();
}
So the database query is successful, that was tested with the commented out system.out.println. It is the following lines that end up throwing the error:
en_prop.store(en_os, null);
fr_prop.store(fr_os, null);
Update: I did a search on the java.util.Properties which lead me to the javadocs on it and wow does that simplify many things. I can now grab a property value or check if the key exists in 6 lines of code (not counting try catch).
Properties prop = null;
InputStream is = null;
this.prop = new Properties();
is = this.getClass().getResourceAsStream(path);
prop.load(is);
this.prop.getProperty("key name"); //returns value of key, or null
this.prop.containsKey("key name"); //returns true if key exists
Update2: There is an issue using java.util.Properties and that is you lose all formatting of the original file, so white-space, comments, and ordering are all lost. In another answer someone suggested using Apache's Commons Configuration API. I plan on trying it out.
So I ended up creating a class to handle interactions with the ApplicationResources(_fr).properties files instead of doing it in the DAO. This was because I plan on using it in more places. I also started using methods from the java.util.Properties Javadocs which proved very helpful and simplified many areas.
Below is my new file write/properties store code.
try (
OutputStream en_os = new FileOutputStream(getClass().getResource(en_path).getFile(),false);
OutputStream fr_os = new FileOutputStream(getClass().getResource(fr_path).getFile(), false);
)
{
en_prop.store(en_os, null);
fr_prop.store(fr_os, null);
} catch (IOException e) {
e.printStackTrace();
}
Lets compare the new and original OutputStreams:
OutputStream en_os = new FileOutputStream(getClass().getResource(en_path).getFile(),false); //new
OutputStream en_os = new FileOutputStream(en_path); //original, Access is Denied
This answer is incomplete for the following reasons.
I am unable to explain why the original method failed and resulted in a "Access is denied error".
More concerning reason to me, this doesnt actually alter the file I am expecting or wanting. I expected to alter the file that appears in my project navigator, but when viewed changes are not observed. If I use an absolute path (C:\...) and overwrite the file then I can alter it as expected, but this path would have to be changed as servers are changed and its bad programming and dangerous. This working method is altering some kind of temp or running file (as confirmed via the path as the file that shows the new values is in the tmp0 folder). After some testing, this temporary file is overwritten on startup only when the original file has been changed, otherwise the new values persist across application starting.
I am also unsure as to the scope of this file. I am unable to tell if all users interacting with the website would cause changes to the same file. If all users are interacting with the file, then potential leakage across sessions could occur. It is also possible that each session has isolated values and could lead to missing information. I suspect that all users are interacting with the same resource but have not performed the testing required to be absolutely positive about this. UPDATE: I have confirmed that all users interact with the same temporary file.
Solved
my problem was that my imagelink string was null sometimes, and i was bcz of bad getting data!
I have an image link which i want to download it into bitmap, this but i cant because the bitmapdownloader UniversalImageLoader both shows null, but the link is perfectly correct and working.
Im getting them from a wordpress website which the link is:
http://wikirap.ir/?get_all
i face this problem sometimes for some post photos in website. so i faced it again for a post photo(i mean the imgloaders was showing null) and i decided to put another photo(which it was loading correctly in app) for the post! but guess what? it didnt load too! but when i was opening the other post with same photo(in app) its loading!
this problem is making me crazy badly!
Update:
i changed my bitmap download code but my first code was saying this error
"... Bitmap.getconfig() on null object reference"
Help me to get rid of this please.
The thing i found out is that the photo shows as preview photo in list but when i click to see details it shows blank!(really bullshit)
Update Code :
Json getting : well i get it with okhttp and its ok and no need guys.
Showing in list: I use json and lazy list adapter which is working perfect and showing all photos even those i cant load them in detail page.
(preview image link is the same as detail image link)
detail page code getting bitmap:
protected void onPostExecute(String file_url) {
try {
actionBar.setTitle(name1.toString() + " - " + name2.toString());
tvinfo.setText(likes + " Likes | " + views + " Views");
}catch (NullPointerException e)
{
}
try
{
/**
URL url = new URL(Imagelink);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmaporigin = BitmapFactory.decodeStream(input);
**/
imgLoadercover imglcover = new imgLoadercover(ac.getApplicationContext());
imglcover.DisplayImage(Imagelink, R.color.placeholder, cover);
}catch (Exception e)
{
}
}
I am battling with this issue for a long time, dug around Google and SO but still no luck. Finally, I am out here to get your help, please advise or help.
My problem with the following source code is that it only displays string content the images do not display instead it shows white rectangle or sometimes a blue image with question mark.
Q: How to display images?
Here is my code:
private void openURL() {
DefaultHttpClient client = new HttpsRequest(getApplicationContext());
HttpGet get = new HttpGet(getUrlField().getText().toString());
// Execute the GET call and obtain the response
HttpResponse getResponse;
try {
getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();
String content = EntityUtils.toString(responseEntity);
getWebView().loadDataWithBaseURL(null, content, "text/html", "utf-8",
null);
} catch (ClientProtocolException e) {
WSLog.e(THIS_FILE, "HTTP Error.");
e.printStackTrace();
} catch (IOException e) {
WSLog.e(THIS_FILE, "Url Load Error.");
e.printStackTrace();
}
}
webView output:
With the help of Vasarat and few modification from my side helped me to answer this question. I have modified the following code line as
getWebView().loadDataWithBaseURL("http://mywebSite.com/parent_dir_to_iamges/", content, "text/html", "UTF-8","about:blank");
This modification gave me the perfect output as expected.
Please fallow the comments to understand details about the issue.
Note: I have used http in the base url instead https.....Please let me know if I can use https. webview.Loadurl() with https url works fine if API level is 10 or above but it shows blank page for API level 8.
I desperately need your expertise in resolving a Windows-7 issue.
Scenario: I have a frame-based Help package that is set up for context-sensitive help calls. A Java application is able to control what page the Help packages opens to by passing a tag representing the desired HTML named anchor to an HTML file called pophelp. This file has javascripting that reads the passed tag from the end of the URL and maps it to the appropriate HTML file in the help package and opens it.
Issue: The above scenario works in Windows XP, but no longer in Windows 7.
Calling mechanism from Java application: rundll32 url.dll,FileProtocolHandler file://filepath/pophelp.html?tag
Summary of findings so far: It appears that url.dll no longer allows parameters to be passed with URLs in Windows 7. Parameters are being stripped. I also tried the same type of call using Desktop.getDesktop().browse() from Java, but it too seems to strip off all parameters after .html.
Sample code:
Original call that works in Windows XP --
Running command: rundll32 url.dll,FileProtocolHandler file://C:\Program Files\App System\App-Editor-8.0.1\help\pophelp.html?TAG=callsubflow
Result: ?TAG=callsubflow is not passed.
New code using Desktop.getDesktop().browse() --
public static void main(String[] args) {
String url = "file:///C:/Program Files/App System/App-Editor-8.0.1/help/pophelp.html?TAG=callsubflow";
try {
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(new URI(url.replace(" ", "%20")));
}
}
} catch (IOException e) {
System.out.println("Unable to open "+url+": "+e.getMessage());
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
Result: ?TAG=callsubflow is not passed.
Any assistance would be appreciated!
I really can't tell why Windows removes parameters on local files. As mentioned in the comments this seams to be some kind of weird restrictions for security. But I once had a similar problem and I found a workaround that fits in this situation as well.
Simply create a local temporary HTML file (without parameters) that redirects you to the desired one (with parameters).Have a look at these two methods:
// creates w3c conform redirect HTML page
public String createRedirectPage(String url){
return "<!DOCTYPE HTML>" +
"<meta charset=\"UTF-8\">" +
"<meta http-equiv=\"refresh\" content=\"1; url=" + url + "\">" +
"<script>" +
"window.location.href = \"" + url + "\"" +
"</script>" +
"<title>Page Redirection</title>" +
"<!-- Note: don't tell people to `click` the link, just tell them that it is a link. -->" +
"If you are not redirected automatically, follow the <a href='" + url + "'>link</a>";
}
// creates temporary file with content of redirect HTML page
public URI createRedirectTempFile(String url) {
BufferedWriter writer = null;
File tmpFile = null;
try {
// creates temporary file
tmpFile = File.createTempFile("pophelp", ".html", null);
// deletes file when the virtual machine terminate
tmpFile.deleteOnExit();
// writes redirect page content to file
writer = new BufferedWriter(new FileWriter(tmpFile));
writer.write(createRedirectPage(url));
writer.close();
}
catch (IOException e) {
return null;
}
return tmpFile.toURI();
}
Now you can use these like this:
String url = "file:///C:/Program Files/App System/App-Editor-8.0.1/help/pophelp.html?TAG=callsubflow";
if (Desktop.isDesktopSupported()) {
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.BROWSE)) {
desktop.browse(createRedirectTempFile(url.replace(" ", "%20")));
}
}
I have a solution, not a quick (or pretty) solution, but a solution nonetheless :)
rundll32 url.dll,FileProtocolHandler does pass params when using URLs with http/s protocol (try rundll32 url.dll,FileProtocolHandler http://www.google.com?q=google), so you can setup small http server (like Jetty i guess) to serve your help files and show them using
rundll32 url.dll,FileProtocolHandler http://localhost:[helpServerIp]/help/pophelp.html?TAG=callsubflow