Error in FileUtils.copyUrlToFile - java

I am executing the below code to pull the .gz file from a URL to a local directory.For small files it goes through fine but for large files it downloads only part of it but does not fail. I get to know the error only when I try to UNZIP it. Can someone throw any light on this on what could be the reason.
public boolean downloadFilemethod(String filePath, String url, String
decompressFilePath) {
if (StringUtils.isNotBlank(filePath) && StringUtils.isNotBlank(url)) {
try {
FileUtils.copyURLToFile(new URL(url), new File(SRC_BASE_DIR + filePath),
TIMEOUT_IN_MILLIS, TIMEOUT_IN_MILLIS);
ingestmethod(filePath,decompressFilePath);
downloadSuccess = true;
}
catch (MalformedURLException e)
{ LOG.warn("some message"); }
catch (IOException e)
{ LOG.warn("some message);
}
}
return downloadSuccess
}

Related

copying directories using .jar program in java

i want to copy directories from the the place where my .jar files exist?
here are what i tried.. but i always get /home/user/
how can i copy files from where my .jar program exist?
private void copy_dir() {
//Path sourceParentFolder = Paths.get(System.getProperty("user.dir") + "/Project/");
// Path sourceParentFolder = Paths.get(Paths.get(".").toAbsolutePath().normalize().toString());
Path destinationParentFolder = Paths.get(System.getProperty("user.home"));
try {
Stream<Path> allFilesPathStream = Files.walk(sourceParentFolder);
Consumer<? super Path> action = new Consumer<Path>() {
#Override
public void accept(Path t) {
try {
String destinationPath = t.toString().replaceAll(sourceParentFolder.toString(), destinationParentFolder.toString());
Files.copy(t, Paths.get(destinationPath));
} catch (FileAlreadyExistsException e) {
//TODO do acc to business needs
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
allFilesPathStream.forEach(action);
} catch (FileAlreadyExistsException e) {
//file already exists and unable to copy
} catch (IOException e) {
//permission issue
e.printStackTrace();
}
}
Try
Path destinationParentFolder = Paths.get(System.getProperty("user.dir"));
"user.dir" gets the absolute path from where your application was initialized.
"user.home" gets the user's home directory.

get the sharedlink of an existing file in Dropbox using the dropBox-sdk-java

I want to retrieve the shared file url of an existing file on Dropbox. I am using the dropbox-java-sdk, and I have managed to create a shared link for a file I just uploaded. The only way I managed to get the shared link of an existing file is by listing all links and get the one I want depending on the path, like so
public void getShareLink(String path) throws DbxApiException, DbxException{
DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
try {
ListSharedLinksResult sharedLinkMetadata = client.sharing().listSharedLinks();
for (SharedLinkMetadata slm: sharedLinkMetadata.getLinks()){
if(slm.getPathLower().equalsIgnoreCase(path)){
System.out.println(slm.getUrl());
return;
}
}
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
} catch (DbxException ex) {
System.out.println(ex);
}
}
Isn’t there a way to directly get the url for the file I want? I just think it is a waste to iterate all items just to get one of them.
Get a ListSharedLinksBuilder from listSharedLinksBuilder and set ListSharedLinksBuilder.withDirectOnly to request only links for the exact path specified:
public String getShareLink(String path) {
DbxRequestConfig config = new DbxRequestConfig("test/DbApi-sdk");
DbxClientV2 client = new DbxClientV2(config, getToken(AuthorizationFile));
try {
ListSharedLinksResult sh = client.sharing().listSharedLinksBuilder()
.withPath(path)
.withDirectOnly(true)
.start();
for (SharedLinkMetadata slm : sh.getLinks()) {
return slm.getUrl();
}
} catch (CreateSharedLinkWithSettingsErrorException ex) {
System.out.println(ex);
return null;
} catch (DbxException ex) {
System.out.println(ex);
return null;
}
return null;
}

Renaming a file on HDFS works in local mode but not in cluster mode

I have an object in charge of opening a file on HDFS to write. This object renames the file it just wrote once the close() method is invoked.
The mechanism works when running in local mode, but it fails to rename the file in cluster mode.
//Constructor
public WriteStream() {
path = String.format("in_progress/file");
try {
OutputStream outputStream = fileSystem.create(new Path(hdfs_path+path), new Progressable() {public void progress() { System.out.print("."); }
});
writer = new BufferedWriter(new OutputStreamWriter(outputStream));
} catch (IOException e) {
e.printStackTrace();
}
}
public void close() {
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Did you experience that before ?
Apparently FileSystem.rename(Path) creates missing directories on the path when executed in local mode, but it does not when run in cluster mode.
This code works in both modes:
public void close() {
String dirPath = String.format("%s_dir/", date, timestamp);
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
fileSystem.mkdir(new Path(hdfs_path+dirPath));
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Just curious, but how can you rename a file that officially doesn't exist (because you're still writing at that point)?
The fix is to rename after the file has been completed. That is, when you invoked the close method.
So your code should look like this:
public void close() {
String newPath = String.format("%s_dir/%s_file", date, timestamp);
try {
writer.close();
fileSystem.rename(new Path(hdfs_path+path), new Path(hdfs_path+newPath));
} catch (IOException e) {
e.printStackTrace();
}
}

open pdf file located in a ressource folder

I'm trying to open a pdf located in a ressource folder with my application.
It does work on the emulator but nothing happens when I try on the exported application.
I'm guessing I'm not using the rigth path but do not see where I'm wrong. The getRessource method works very well with my images.
Here is a code snippet :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported()) {
try {
URL monUrl = this.getClass().getResource(pdf);
File myFile = new File(monUrl.toURI());
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I'm referring to the pdf variable this way : "name_of_the_file.pdf"
Edit: I've pasted the whole method
Ok, solved it. The file being located in a Jar, the only way to get it was through a inputsteam/outstream and creating a temp file.
Here is my final code, which works great :
public void openPdf(String pdf){
if (Desktop.isDesktopSupported())
{
InputStream jarPdf = getClass().getClassLoader().getResourceAsStream(pdf);
try {
File pdfTemp = new File("52502HPA3_ELECTRA_PLUS_Fra.pdf");
// Extraction du PDF qui se situe dans l'archive
FileOutputStream fos = new FileOutputStream(pdfTemp);
while (jarPdf.available() > 0) {
fos.write(jarPdf.read());
} // while (pdfInJar.available() > 0)
fos.close();
// Ouverture du PDF
Desktop.getDesktop().open(pdfTemp);
} // try
catch (IOException e) {
System.out.println("erreur : " + e);
} // catch (IOException e)
}
}
You mentioned that it is running on Emulator but not on the application. There is high probability that the platform on which the application is running does not support Desktop.
Desktop.isDesktopSupported()
might be returning false. Hence no stack trace or anything.
On Mac, you can do:
Runtime runtime = Runtime.getRuntime();
try {
String[] args = {"open", "/path/to/pdfFile"};
Process process = runtime.exec(args);
} catch (Exception e) {
Logger.getLogger(NoJavaController.class.getName()).log(Level.SEVERE, "", e);
}

Creating a step by step validation

I am trying to make a monitoring application for a FTP server using FTP4J(referred to as client in the code example).
It connects to a FTP, logs in, creates a file locally, uploads file, downloads file, validates the files are the same, cleans up files on ftp and locally and disconnects.
All this is already made but my question is how do I best log what has happened and break when an error is detected?
The simple solution I could think of was to make a Boolean that shows if previous steps where successful and only do next step if they where.
StringBuilder sb = new StringBuilder();
boolean noError = true;
// Connect to FTP
try {
client.connect(hostname, port);
} catch (Exception e) {
noError = false;
sb.append("failed to connect<br>");
}
//Logging in to FTP
if(noError) {
try {
client.login(username, password);
} catch (Exception e) {
noError = false;
sb.append("failed to login<br>");
}
}
...
// Close connection
if(client.isConnected()) {
try {
client.disconnect(true);
} catch (Exception e) {
sb.append("failed to disconnect<br>");
}
}
another solution I could think of was nested try/catch but that looked even worse, is there a better way of doing this?
The solution is simple: don't catch the exception. As soon as an exception is thrown and is not caught, the whole process will stop. Or catch it but transform it into your own exception with the appropriate error message, and throw this exception.
Side note: you should use a boolean and not a Boolean to store a non-nullable boolean value.
StringBuilder sb = new StringBuilder();
Boolean noError = true;
// Connect to FTP
try {
client.connect(hostname, port);
client.login(username, password);
} catch (ConnectException ce) {
sb.append("Couldn't connect: ");
sb.append(ce.getMessage);
} catch (LoginException le) {
sb.append("Couldn't login: ");
sb.append(le.getMessage);
} finally {
if(client.isConnected()) {
try {
client.disconnect(true);
} catch (Exception e) {
sb.append("failed to disconnect<br>");
}
}

Categories

Resources