I am working on creating a personal utility downloader for things like Malwarebytes, Adware Cleaner, and etc. But I have never worked with anything like this before. I searched around and found some documentation on how to download files from a URL into a directory, but I haven't been able to get it to work yet. The first time it turned the directory into a file that was unsuable and now that I have changed the URL it is failing to download due to the errors listed at the bottom. Could someone point me in the right direction or tell me what I am doing wrong?
package com.kcc;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
public class Testing2 {
public static String testURL;
public static String saveDir;
public static void main(String[] args) throws IOException {
testURL ="https://download.bleepingcomputer.com/dl/a652734ff3304da2530acb93754c1bf7/5af5a320/windows/security/security-utilities/a/adwcleaner/AdwCleaner.exe";
//"https://download.toolslib.net/download/file/1/1511?s=2LPvu8kniU2T794QD0FXSN21jxnJOqLP";
saveDir = "C:\\Users\\Austin\\Desktop\\kccutil";
download(testURL, saveDir);
}
private static Path download(String sourceURL, String targetDirectory) throws IOException
{
URL url = new URL(sourceURL);
String fileName = sourceURL.substring(sourceURL.lastIndexOf('/') + 1, sourceURL.length());
Path targetPath = new File(targetDirectory + File.separator + fileName).toPath();
Files.copy(url.openStream(), targetPath, StandardCopyOption.REPLACE_EXISTING);
return targetPath;
}
}
I am currently getting these errors
Exception in thread "main" java.nio.file.NoSuchFileException: C:\Users\Austin\Desktop\kccutil\AdwCleaner.exe
at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:79)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(WindowsFileSystemProvider.java:230)
at java.nio.file.spi.FileSystemProvider.newOutputStream(FileSystemProvider.java:434)
at java.nio.file.Files.newOutputStream(Files.java:216)
at java.nio.file.Files.copy(Files.java:3016)
at com.kcc.Testing2.download(Testing2.java:25)
at com.kcc.Testing2.main(Testing2.java:17)
EDIT: For the error above, turns out the directory wasn't created. But now I am receiving a new error
Exception in thread "main" java.io.FileNotFoundException: https://download.bleepingcomputer.com/dl/a652734ff3304da2530acb93754c1bf7/5af5a320/windows/security/security-utilities/a/adwcleaner/AdwCleaner.exe
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1872)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
at java.net.URL.openStream(URL.java:1045)
at com.kcc.Testing2.download(Testing2.java:25)
at com.kcc.Testing2.main(Testing2.java:17)
For the debugging purposes you can try saving files to working directory (i.e. refer to . folder). Using this approach you can access file by it's filename.
For the future I recommend you using Java 7 NIO Api: Paths.get() - for originally building path from parts, path.parent() - to refer to parent directory, path.resolve() - to build child path.
If you want to download a file you should use FTP server and not HTTP in case you have the executable.
But if you already have an HTTP link on web who calls a downloadable .exe (Like in your case), you don't really need a download method. You just need to send an http request to navigator (preferably if it is a web app), something like:
File htmlFile = new File(url);
Desktop.getDesktop().browse(htmlFile.toURI());
Or you can download that file using Apache Common IO's FileUtils:
import org.apache.commons.io.FileUtils;
FileUtils.copyURLToFile(url, file_destination);
Or you can check this response using Java NIO
Related
Files.readString(Paths.get(ClassPathResource("temp.md").uri)
The markdown text in the src > main > resources folder is loaded through the code above. In the local environment, the file location is checked and data is loaded normally, but when the built jar is executed on ec2, the following error is returned.
I think, the path to the built jar is wrong, but I don't know how to solve it, please advise
File loading from files located in the classpath work differently when the application is packed as a JAR.
Check this Baeldung article for the detailed explanation.
This will work independent from the way the code is packaged:
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.springframework.util.ResourceUtils;
import java.nio.file.Files;
#UtilityClass
public class FileTestUtils {
#SneakyThrows
public byte[] getFileAsBytes(String fileName) {
return Files.readAllBytes(ResourceUtils.getFile("classpath:" + fileName).toPath());
}
#SneakyThrows
public String getFileAsString(String fileName) {
return new String(Files.readAllBytes(ResourceUtils.getFile("classpath:" + fileName).toPath()));
}
}
Alternatively we can read resource using classloader instance.
ClassLoader classLoader = SpringBootResourcesApplication.class.getClassLoader();
File file = new File(classLoader.getResource("temp.md").getFile());
I'm working on a very simple project that's supposed to open an image with the windows video player when run. However, I've encountered a problem. I want to have it be able to access the file "snp.jpg" with a relative file path, so it will work on computers other than my own. But, when I have it set to a absolute file path, it fails and tells me that "the file ... does not exist". Any Ideas?
import java.awt.Desktop;
import java.io.File;
public class openpic {
public static void main (String args[]) throws Exception
{
File f = new File ("C:\Users\charl\Desktop\Computer Science\JavaProjects\src\snp.png");
Desktop d = Desktop.getDesktop();
d.open(f);
System.out.println("imageviewer open;");
}
}
(Ops... fixing the answer, after I read the text above the code)
The relative path will start from the directory you run the program. Also called current working directory.
Also, as you are using Files, try to use the NIO API, with Path. Like:
Path filePath = Paths.get("./snp.png")
With this API, you can check the working directory using:
filePath.toAbsolutePath()
// just print it then, or check with a debugger
Also, be careful about the slashes.
When using Windows and this slash \, you need to make them double: \\.
Other option is to invert it: /.
Microsoft Windows syntax
import java.awt.Desktop;
import java.io.File;
public class openpic {
public static void main (String args[]) throws Exception
{
// Microsoft Windows syntax
File f = new File ("C:\\Users\\charl\\Desktop\\Computer Science\\JavaProjects\\src\\snp.png");
Desktop d = Desktop.getDesktop();
d.open(f);
System.out.println("imageviewer open;");
}
}
I'm using Java code to download a file from the Internet and save it to some directory.
However, the code downloads the HTML source code of the page instead of the file contents.
The code below illustrates the problem:
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
public class JavaFileDownloadTest
{
public static void download(String remoteURL, String targetFilePath)
throws IOException
{
URL downloadableFile = new URL(remoteURL);
ReadableByteChannel readableByteChannel = Channels.newChannel(downloadableFile.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(targetFilePath);
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
}
public static void main(String[] arguments) throws IOException
{
String userHome = System.getProperty("user.home");
String fileName = "Test.txt";
String targetFilePath = userHome + File.separator + "Downloads" + File.separator + fileName;
download("http://bullywiiplaza.cuccfree.com/" + fileName, targetFilePath);
Desktop.getDesktop().open(new File(targetFilePath));
}
}
The file located here contains the text
Hello StackOverflow!
However, when downloaded using the above code, I'm getting the HTML source code as file content instead:
<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("ae71113e4baf38cee1c1aacf0ae66c00");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="http://bullywiiplaza.cuccfree.com/Test.txt?ckattempt=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
Why is this and how do I fix it? I already tried various libraries and methods for downloading files but all of them yielded this same "faulty" result.
I think the target url executes some javascript to provide the file. This script has to be interpreted (and executed) by some javascript engine.
So you need either some resolution to get the real file url (and not just the javascript) or integrate some javascript engine to execute the script code and get the result.
I think this could help you: Executing javascript in java - Opening a URL and getting links
or better:
http://www.java2s.com/Code/Java/JDK-6/ExecuteJavascriptscriptinafile.htm
I switched the website hoster to this one and now the code from above works as expected.
http://bullywiiplaza.cuccfree.com/Test.txt doesn't exist. I think the url should be https://bullywiiplaza.cuccfree.com/Test.txt which exists.
I'm nearing the end of a program development for my computer science course. However, one of the requirements is to have a user manual within the app. I saved the user manual as a PDF inside Eclipse's workspace.
It is stored under "/Documents/PDF Manual.pdf". I originally used this code:
URL url = getClass().getResource( fileSeparator + "Documents" + fileSeparator + "PDF Manual.pdf");
//fileSeparator = '/' on mac, & '\\' on windows
File userManual = new File (url.toURI());
if (userManual.exists())
{
Desktop.getDesktop().open(userManual);
}
This works fine while running the project from eclipse, however URI returns a non hierarchical exception (as expected) when the program is exported to a jar file. I thought of playing around with url.toString(), using substring and replaceAll() to get rid of unwanted characters (%20 for the space), however this gave weird results and of course the code wouldn't work properly when it wasn't a jar file.
I looked at InputStream, however this is only used to read from a file and I cannot open the file using a desktop app.
Due to the process of submission, the pdf HAS to be saved inside the project folders.
Also, my code has to be platform independent (or at the very least, work on windows and mac) and thus manipulating file names becomes a lot more complicated. Any suggestions?
Edit:
After #SubOptimal 's help, this is the code I am now using:
String inputPdf = "Documents" + fileSeparator + "PDF Manual.pdf";
InputStream manualAsStream = getClass().getClassLoader().getResourceAsStream(inputPdf);
Path tempOutput = Files.createTempFile("TempManual", ".pdf");
tempOutput.toFile().deleteOnExit();
Files.copy(manualAsStream, tempOutput, StandardCopyOption.REPLACE_EXISTING);
File userManual = new File (tempOutput.toFile().getPath());
if (userManual.exists())
{
Desktop.getDesktop().open(userManual);
}
This works on mac. However, on windows manualAsStream is null for some unknown reason.
Full working example. Tested in Windows environment.
file structure
.\REPL.java
.\doc\manual.pdf
.\manifest.mf
REPL.java
package sub.optimal;
import java.io.InputStream;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.awt.Desktop;
public class REPL {
public static void main(String[] args) throws Exception {
String inputPdf = "doc/manual.pdf";
Path tempOutput = Files.createTempFile("TempManual", ".pdf");
tempOutput.toFile().deleteOnExit();
System.out.println("tempOutput: " + tempOutput);
try (InputStream is = REPL.class.getClassLoader().getResourceAsStream(inputPdf)) {
Files.copy(is, tempOutput, StandardCopyOption.REPLACE_EXISTING);
}
Desktop.getDesktop().open(tempOutput.toFile());
}
}
manifest.mf
Main-Class: sub.optimal.REPL
compile
javac -d . REPL.java
create JAR
mkdir dist\
jar cvfm dist/REPL.jar MANIFEST.MF sub/optimal/REPL.class doc/manual.pdf
execute JAR
cd dist\
java -jar REPL.jar
Use getResourceAsStream instead of getResource
To write in the temp directory use createTempFile
I have created a java program in eclipse and I am now ready to export it as a jar. My program uses an image file and an executable file. When testing my program in eclipse I referred to these file with a full path, which I obviously cannot do for the jar. Therefore, I changed them like this:
public static final String DRIVERLOC = "./resources/IEDriverServer.exe";
//some other code
File file = new File(DRIVERLOC);
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
and
File pic = new File("./images/Open16.gif");
openButton = new JButton("Select the Text File", createImageIcon(pic.getAbsolutePath()));
I put the images and the resources and images directory in the same directory with the jar. Now for some reason when I run the jar the IEDriverServer works fine but the image does not work and the error is that it cannot find the image. I am confused since I cannot seems to tell the difference. I also used "images/Open16.gif" which did not work either. Why would one work but the other does not? What is the easiest way to fix this?
We do this exact same thing with Selenium Drivers.
What you need to do is take the executable file out of the jar and put it some where windows can run it. If you try and open a jar/zip in Windows Explorer and then double click the .exe inside of a jar/zip, windows will extract the file to a temp directory, and then run it. So do the same thing:
import org.apache.commons.io.IOUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TestClass {
public static void main (String[] args) throws IOException {
InputStream exeInputStream = TestClass.class.getClassLoader().getResourceAsStream("resources/IEDriverServer.exe");
File tempFile = new File("./temp/IEDriverServer.exe");
OutputStream exeOutputStream = new FileOutputStream(tempFile);
IOUtils.copy(exeInputStream, exeOutputStream);
// ./temp/IEDriverServer.exe will be a usable file now.
System.setProperty("webdriver.ie.driver", tempFile.getAbsolutePath());
}
}
Let's say you save the jar and make it run this main function by default.
Running C:\code\> java -jar TestClass.jar Will run the jar from the C:\code directory. It will create the executable at C:\code\temp\IEDriverServer.exe
With your path set to "./resources/IEDriverServer.exe" you are referring to a file on the hard drive "." which does not exist.
You need to get the path of your .jar-file.
You can do this by using
System.getProperty("java.class.path")
This can return multiple values, seperated by semi-colons. The first one should be the folder your jar is in.
You can also use
<AnyClass>.class.getProtectionDomain().getCodeSource().getLocation()
I hope this helps :)
EDIT:
// First, retrieve the java.class.path property. It returns the location of all jars /
// folders (the one that contains your jar and the location of all dependencies)
// In my test it returend a string with several locations split by a semi-colon, so we
// split it and only take the first argument
String jarLocation = System.getProperty("java.class.path").split(";")[0];
// Then we want to add that path to your ressource location
public static final String DRIVERLOC = jarLocation + "/resources/IEDriverServer.exe";
//some other code
File file = new File(DRIVERLOC);
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
You can read about this here.