I can't figure out how to reference a specific line of text in a txt file. I need a specific image from a specific list of URL's and I am trying to generate the URL's by concatenating a URL prefix and a search number from a list of numbers in a txt file. I can't figure out how to reference the txt file and get a string from a line number.
package getimages;
public class ExtractAllImages {
public static int url_to_get = 1;
public static String urlupc;
public static String urlpre = "http://urlineedimagefrom/searchfolder/";
public static String url2 = "" + urlpre + urlupc + "";
public static void main(String args[]) throws Exception {
while(url_to_get > 2622){
String line = Files.readAllLines(Paths.get("file_on_my_desktop.txt")).get(url_to_get);
urlupc = line;
url2 = "" + urlpre + urlupc + "";
String webUrl = url2;
URL url = new URL(webUrl);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
parser.parse(br, callback, true);
for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next()) {
AttributeSet attributes = iterator.getAttributes();
String imgSrc = (String) attributes.getAttribute(HTML.Attribute.SRC);
if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".jpeg")) || (imgSrc.endsWith(".bmp")) || (imgSrc.endsWith(".ico")))) {
try {
downloadImage(webUrl, imgSrc);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
}
public static String right(String value, int length) {
return value.substring(value.length() - length);}
private static void downloadImage(String url, String imgSrc) throws IOException {
BufferedImage image = null;
try {
if (!(imgSrc.startsWith("http"))) {
url = url + imgSrc;
} else {
url = imgSrc;
}
String webUrl = url2;
String imagename = right(webUrl , 12);
imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
String imageFormat = null;
imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
String imgPath = null;
imgPath = "C:/Users/Noah/Desktop/photos/" + urlupc + ".jpg";
URL imageUrl = new URL(url);
image = ImageIO.read(imageUrl);
if (image != null) {
File file = new File(imgPath);
ImageIO.write(image, imageFormat, file);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
My error is in setting the String line, My txt file has 2622 lines and I can't reference the file on my desktop and im not sure how to set the file path to my desktop? Sorry I'm not good at java.
Thanks for any help.
From what I understood you don't know how to access a file that is on your Desktop. Try doing this:
String path = "C:\\Users\\" + System.getProperty("user.name") + "\\Desktop\\" + fileName + ".txt";
Let me explain. We are using
System.getProperty("user.name")
to get the name of the user, if you don't want to use it then you can replace it with the name of your user. Then we are using
"\\Desktop\\"
to acess the Desktop, and finally we are adding the
fileName + ".txt"
to access the file we want that has the extension '.txt'.
Related
I've made a web project with eclipse to run my webservice on Tomcat v7. I have a config file that have to define some variables that should be changed while webserver is up without need of restart it. The problem is: where I can put this file?
At the moment I've put it into the "./" dir, but seems that when I export the WAR file and install it on the webserver it doesn't works. Is there a way to create this file and modify it runtime inside the WAR?
Here is part of the code of the .class file that accesses the config file
public class ToolConfigurations {
private static final Logger log = LogManager.getLogger(ToolConfigurations.class); //Oggetto logging
private static ToolConfigurations instance = null; //verificarne la necessità
private static String defaultServerName = null;
private static String defaultDbName = "postgres";
private static String defaultDbUser = "postgres";
private static String defaultDbPass = "password";
private static int defaultDbMaxConnNum = 10;
private static String configFilePath = ".\\";
private static String configFileName = "tool.properties";
private String serverName = null;
private String dbName = null;
private String dbUser = null;
private String d`enter code here`bPass = null;
private int dbMaxConnNum = -1;
private ToolConfigurations() throws Exception {
File file = new File(configFilePath + configFileName);
if(file.exists()) {
//file configurazione esiste
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String line = null;
while((line = br.readLine()) != null) { //Leggo riga per riga il file
String[] values = line.split(" ");
switch (values[0]) {
case "serverName":
serverName = values[1];
break;
case "dbName":
dbName = values[1];
break;
case "dbUser":
dbUser = values[1];
break;
case "dbPass":
dbPass = values[1];
break;
case "dbMaxConnNum":
dbMaxConnNum = Integer.parseInt(values[1]);
break;
default:
log.warn("Elemento inaspettato nel file di configurazione: " + values[0]);
break;
}
}
br.close();
in.close();
}else {
if(file.createNewFile() == false) {
//Errore creazione file
log.error("Errore creazione file di configurazione");
throw new Exception("Errore creazione file configurazione");
}
//CREO FILE CONFIGURAZIONE CON IMPOSTAZIONI DI DEFAULT
FileWriter fw = new FileWriter(file);
fw.write("dbName " + defaultDbName + "\r\n");
fw.write("dbUser " + defaultDbUser + "\r\n");
fw.write("dbPass " + defaultDbPass + "\r\n");
fw.write("dbMaxConnNum " + defaultDbMaxConnNum + "\r\n");
fw.flush();
fw.close();
log.warn("File di configurazione non trovato. Path: " + file.getAbsolutePath() + ". Creato nuovo file con configurazioni di default.");
//CARICO IMPOSTAZIONI DI DEFAULT
dbName = defaultDbName;
dbUser = defaultDbUser;
dbPass = defaultDbPass;
dbMaxConnNum = defaultDbMaxConnNum;
}
}
public static synchronized ToolConfigurations getInstance() throws Exception {
if(instance == null) {
instance = new ToolConfigurations();
}
return instance;
}
...
In web server, files other than .class are directly taken in charge by the server. You don't need to reload the server.
But try using .properties files and check the structure of the WAR when deployed on the server.
The best solution I found was to a ClassLoader to search for the config file that I have previously created into the main directory.
The example of use is:
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream fileInput = classLoader.getResourceAsStream(configFileName);
if(fileInput == null){
//File not found
}else{
//File found
InputStreamReader sr = new InputStreamReader(fileInput);
BufferedReader br = new BufferedReader(sr);
try {
while((line = br.readLine()) != null) {
//Read file line by line
}
}catch (IOException e) {
throw new IOException("Error parsing file: " + e.getMessage());
}
}
I am Referencing an array to generate urls to download images from however if the url is dead the program errors and stops i need it to skip that url if an error occurs but I can not figure out how any ideas?
package getimages2;
public class Extractimages {
public static String url_to_get = "http://www.url.com/upc/11206007076";
public static long urlupc;
public static String URLarray[] = new String[3000];
public static void main(String args[]) throws Exception {
for(int apples = 0; apples<URLarray.length; apples++){
String UPCarray[] = {"051000012616","051000012913","051000012937"};
url_to_get = "http://www.url/upc/" + UPCarray[apples];
String webUrl = url_to_get;
String url2 = url_to_get;
URL url = new URL(webUrl);
URLConnection connection = url.openConnection();
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
HTMLEditorKit htmlKit = new HTMLEditorKit();
HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
HTMLEditorKit.Parser parser = new ParserDelegator();
HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
parser.parse(br, callback, true);
for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG); iterator.isValid(); iterator.next()) {
AttributeSet attributes = iterator.getAttributes();
String imgSrc = (String) attributes.getAttribute(HTML.Attribute.SRC);
if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".jpeg")) || (imgSrc.endsWith(".bmp")) || (imgSrc.endsWith(".ico")))) {
try {
downloadImage(webUrl, imgSrc);
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}
}
}
public static String right(String value, int length) {
return value.substring(value.length() - length);}
private static void downloadImage(String url, String imgSrc) throws IOException {
BufferedImage image = null;
try {
if (!(imgSrc.startsWith("http"))) {
url = url + imgSrc;
} else {
url = imgSrc;
}
String webUrl = url_to_get;
String imagename = right(webUrl , 12);
imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
String imageFormat = null;
imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
String imgPath = null;
imgPath = "C:/Users/Noah/Desktop/photos/" + imagename + ".jpg";
URL imageUrl = new URL(url);
image = ImageIO.read(imageUrl);
if (image != null) {
File file = new File(imgPath);
ImageIO.write(image, imageFormat, file);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
The program runs through the array fine but once it generates a dead url it errors and exits i need it to skip that url and move on.
Thanks for any help.
Well, you'll need to have a way to check if the URL you've generated is valid. There's some answers on that here:
How to check for a valid URL in Java?
How do I go about extracting the image type from a url using JSoup? I'm parsing an html, and it can send in the image url ( using asbUrl() ). However, I need to test it for its type. Right now it makes everything become a .png, which obviously doesn't work for most types. Any ideas?
First save the image in a file.
Here some codes that can help you do that:
public class DownloadImages {
//The url of the website. This is just an example
private static final String webSiteURL = "http://www.supercars.net/gallery/119513/2841/5.html";
//The path of the folder that you want to save the images to
private static final String folderPath = "<FOLDER PATH>";
public static void main(String[] args) {
try {
//Connect to the website and get the html
Document doc = Jsoup.connect(webSiteURL).get();
//Get all elements with img tag ,
Elements img = doc.getElementsByTag("img");
for (Element el : img) {
//for each element get the srs url
String src = el.absUrl("src");
System.out.println("Image Found!");
System.out.println("src attribute is : "+src);
getImages(src);
}
} catch (IOException ex) {
System.err.println("There was an error");
Logger.getLogger(DownloadImages.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void getImages(String src) throws IOException {
String folder = null;
//Exctract the name of the image from the src attribute
int indexname = src.lastIndexOf("/");
if (indexname == src.length()) {
src = src.substring(1, indexname);
}
indexname = src.lastIndexOf("/");
String name = src.substring(indexname, src.length());
System.out.println(name);
//Open a URL Stream
URL url = new URL(src);
InputStream in = url.openStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream( folderPath+ name));
for (int b; (b = in.read()) != -1;) {
out.write(b);
}
out.close();
in.close();
}
When you have the image saved on your disk use you can findout their extension like this:
String extension = FilenameUtils.getExtension("/path/to/file/image.png");
When you finish, delete the files also using Java.
I am unsure how to take the extension directly from the URL
I am creating a application that will download a lot of files from my own web server. But idk why it is not working. It dont have any response..
Here is some part of my code
Downloader.class
private Proxy proxy = Proxy.NO_PROXY;
public void downloadLibrary()
{
System.out.println("Start downloading libraries from server...");
try
{
URL resourceUrl = new URL("http://www.example.com/libraries.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(resourceUrl.openConnection(proxy).getInputStream());
NodeList nodeLst = doc.getElementsByTagName("Contents");
for (int i = 0; i < nodeLst.getLength(); i++)
{
Node node = nodeLst.item(i);
if (node.getNodeType() == 1)
{
Element element = (Element)node;
String key = element.getElementsByTagName("Key").item(0).getChildNodes().item(0).getNodeValue();
File f = new File(launcher.getWorkingDirectory(), key);
downloadFile("http://www.example.com/" + key, f, "libraries");
}
}
}
catch(Exception e)
{
System.out.println("Error was found when trying to download libraries file " + e);
}
}
public void downloadFile(final String url, final File path, final String fileName)
{
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
{
#Override
protected Void doInBackground() throws Exception
{
launcher.println("Downloading file " + fileName + "...");
try
{
URL fileURL = new URL(url);
ReadableByteChannel rbc = Channels.newChannel(fileURL.openStream());
FileOutputStream fos = new FileOutputStream(path);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
}
catch(Exception e)
{
System.out.println("Cannot download file : " + fileName + " " + e);
}
return null;
}
#Override
public void done()
{
System.out.println(fileName + " had downloaded sucessfully");
}
};
worker.execute();
}
Here is some part of my xml file(libraries.xml)
<Key>libraries/org/lwjgl/lwjgl/lwjgl/2.9.0/lwjgl-2.9.0.jar</Key>
My idea is, my application will read the XML file. Then it will download file from the server and save to computer. For example, my application download http://www.example.com/libraries/org/lwjgl/lwjgl/lwjgl/2.9.0/lwjgl-2.9.0.jar then it will save to C://WorkingDir/libraries/org/lwjgl/lwjgl/lwjgl/2.9.0/lwjgl-2.9.0.jar
There is ton of <Key></Key> in my XML file and i have to download all of it.
Is that any code wrong? Thanks for helping.
Try consuming the connection directly through a reader of some sort to a String first, then you can manipulate that however you need.
package come.somecompany.somepackage.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class WebUtils {
/**
* Gets the HTML value of a website and
* returns as a string value.
*
* #param website website url to get.
* #param ssl True if website is SSL.
* #param useragent Specified User-Agent (empty string "" means use system default).
* #return String value of website.
*/
public String getHTML(String website, boolean ssl, String useragent) {
String html = "";
String temp;
String prefix;
if (ssl) {
prefix = "https://";
} else {
prefix = "http://";
}
try {
URL url = new URL(prefix + website);
URLConnection con = url.openConnection();
if (!(useragent.equalsIgnoreCase(""))) {
con.setRequestProperty("User-Agent", useragent);
}
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
while((temp = in.readLine()) != null) {
html += temp + "\n";
}
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return html;
}
}
Also, it sort of looks like you are attempting to parse HTML using an XML pattern... which may give you difficulties. You could try out JSoup - it's a java HTML parser, and works pretty well and is easy: http://jsoup.org/
It may help with consuming documents from your website without needing to build your own downloader too.
UPDATE --
Try reading into a BufferedReader, perhaps your program isn't receiving the full document, buffered reader may help.
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
So with your first method, something like:
public void downloadLibrary()
{
System.out.println("Start downloading libraries from server...");
try
{
URL resourceUrl = new URL("http://www.example.com/libraries.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// change here
URLConnection con = resourceUrl.openConnection();
BufferedReader bfr = new BufferedReader(new InputStreamReader(con.getInputStream()));
String tempDoc = "";
String tempStr;
while (tempStr = bfr.readLine()) != null) {
tempDoc += tempStr + System.getProperty("line.separator");
}
Document doc = db.parse(tempDoc);
NodeList nodeLst = doc.getElementsByTagName("Contents");
for (int i = 0; i < nodeLst.getLength(); i++)
{
Node node = nodeLst.item(i);
if (node.getNodeType() == 1)
{
Element element = (Element)node;
String key = element.getElementsByTagName("Key").item(0).getChildNodes().item(0).getNodeValue();
File f = new File(launcher.getWorkingDirectory(), key);
downloadFile("http://www.example.com/" + key, f, "libraries");
}
}
}
catch(Exception e)
{
System.out.println("Error was found when trying to download libraries file " + e);
}
}
I already know where the image is, but for simplicity's sake I wanted to download the image using JSoup itself. (This is to simplify getting cookies, referrer, etc.)
This is what I have so far:
//Open a URL Stream
Response resultImageResponse = Jsoup.connect(imageLocation).cookies(cookies).ignoreContentType(true).execute();
// output here
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(new java.io.File(outputFolder + name));
//BufferedWriter out = new BufferedWriter(new FileWriter(outputFolder + name));
out.write(resultImageResponse.body()); // resultImageResponse.body() is where the image's contents are.
out.close();
I didn't even finish writing the question before I found the answer via JSoup and a little experimentation.
//Open a URL Stream
Response resultImageResponse = Jsoup.connect(imageLocation).cookies(cookies)
.ignoreContentType(true).execute();
// output here
FileOutputStream out = (new FileOutputStream(new java.io.File(outputFolder + name)));
out.write(resultImageResponse.bodyAsBytes()); // resultImageResponse.body() is where the image's contents are.
out.close();
Simply you can use these methods-
public static String storeImageIntoFS(String imageUrl, String fileName, String relativePath) {
String imagePath = null;
try {
byte[] bytes = Jsoup.connect(imageUrl).ignoreContentType(true).execute().bodyAsBytes();
ByteBuffer buffer = ByteBuffer.wrap(bytes);
String rootTargetDirectory = IMAGE_HOME + "/"+relativePath;
imagePath = rootTargetDirectory + "/"+fileName;
saveByteBufferImage(buffer, rootTargetDirectory, fileName);
} catch (IOException e) {
e.printStackTrace();
}
return imagePath;
}
public static void saveByteBufferImage(ByteBuffer imageDataBytes, String rootTargetDirectory, String savedFileName) {
String uploadInputFile = rootTargetDirectory + "/"+savedFileName;
File rootTargetDir = new File(rootTargetDirectory);
if (!rootTargetDir.exists()) {
boolean created = rootTargetDir.mkdirs();
if (!created) {
System.out.println("Error while creating directory for location- "+rootTargetDirectory);
}
}
String[] fileNameParts = savedFileName.split("\\.");
String format = fileNameParts[fileNameParts.length-1];
File file = new File(uploadInputFile);
BufferedImage bufferedImage;
InputStream in = new ByteArrayInputStream(imageDataBytes.array());
try {
bufferedImage = ImageIO.read(in);
ImageIO.write(bufferedImage, format, file);
} catch (IOException e) {
e.printStackTrace();
}
}