Get file time before downloading the file using FTP - java

Any way to get the file creation date or last modification date without using getModificationTime in Java.
I'm using org.apache.commons.net.ftp.FTPClient class. My issue is, I'm unable to use getModificationTime to get the time stamp before downloading the file..

You can use Apache Commons Net library, here is a sample code:
package com.grebski.ftp;
import org.apache.commons.net.ftp.FTPClient;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
public class FtpTest {
public static void main(String[] args) throws IOException {
String ftpUrl = "speedtest.tele2.net";
FTPClient ftpClient = new FTPClient();
ftpClient.connect(ftpUrl);
ftpClient.user("anonymous");
ftpClient.pass("anonymous#a.com");
Arrays.stream(ftpClient.listFiles()).forEach(file -> {
LocalDateTime creationDateTime = Instant.ofEpochMilli(file.getTimestamp().getTimeInMillis()).atZone(ZoneId.systemDefault()).toLocalDateTime();
String msg = String.format("%s %s", file.getName(), creationDateTime);
System.out.println(msg);
}
);
}
}

Did you get a chance to find the size of the file and the network speed? They are important aspects to learn the download time.

Related

Trying to run a server with multiple clients where the client will tell me the date but my java.util.Date is not importing correctly

Hi I am currently doing a project where I have multiple clients telling the server a date however my java.util.date is not working and running me back an error message.
How would I fix this issue code is provided below as well as the message. I must also add I am writing said project in java and using the eclipse IDE.
package clientServer;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
public class Server {
public static void main(String[] args) throws IOException {
try (ServerSocket listener = new ServerSocket(7000)) {
System.out.println("The date server is running...");
while (true) {
try (Socket socket = listener.accept()) {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
}
}
}
}
}
the 2 errors
for import java.util.Date it says import java.util.Date cannot be resolved
and for the code out.println(new Date()) it says Date cannot be resolved into a type
I have tried cleaning the project and using alt 5 to give it a direct path however this has not worked

Can't parse an HTML element from web page by loop in, after some time parsing in java

I receive different exceptions after some iteration in runtime when I try to parse page and get the same element every 10 seconds.
javax.net.ssl.SSLHandshakeException: Remote host terminated the handshake
Or EOFexception, SocketException, UnknownExceptiin.
In addition I am using maven for project and Jsoup for parsing, and I have last version of JDK (however I don't think it'll help). Here is my code.
package com.download;
import java.io.IOException;
import java.net.URL;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.Jsoup;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
public class Download
{
public static void download() throws IOException,InterruptedException
{
for(int i=0;i<120;i++)
{
System.out.println(Parser.parse());
Thread.sleep(10000);
}
}
}
public class Parser
{
public static String parse( ) throws IOException
{
String data=parseElement();
String time=getDate();
return data+" "+time;
}
private static String parseElement() throws IOException
{
String url = "https://www.example.com/domain-has-changed-here";
Document page = Jsoup.connect(url).timeout(0).get();
return page.getElementById("last_last").text();
}
private static String getDate() throws IOException
{
LocalTime myDateObj = LocalTime.now();
DateTimeFormatter myFormatObj = DateTimeFormatter.ofPattern("HH:mm:ss");
String time = myDateObj.format(myFormatObj);
return time;
}
}
Have any Ideas? Thanks in advance.
This is probably due to the site that detected you as spam, to reduce this risk I recommend you customize your connection depending on the site, here is an example of customization:
Connection connection = Jsoup.connect("https://www.example.com/domain-has-changed-here");
connection.userAgent("Mozilla");
connection.timeout(5000);
connection.cookie("cookiename", "val234");
connection.cookie("cookiename", "val234");
connection.referrer("http://google.com");
connection.header("headersecurity", "xyz123");
Document docCustomConn = connection.get();

Azure functions using Java - How to created #BlobTrigger

i need to created Azure function BlobTrigger using Java to monitor my storage container for new and updated blobs.
tried with below code
import java.util.*;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.*;
import java.nio.file.*;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*;
#FunctionName("testblobtrigger")
public String testblobtrigger(#BlobTrigger(name = "test", path = "testcontainer/{name}") String content) {
try {
return String.format("Blob content : %s!", content);
} catch (Exception e) {
// Output the stack trace.
e.printStackTrace();
return "Access Error!";
}
}
when executed it is showing error
Storage binding (blob/queue/table) must have non-empty connection. Invalid storage binding found on method:
it is working when added connection string
public String kafkablobtrigger(#BlobTrigger(name = "test", path = "testjavablobstorage/{name}",connection=storageConnectionString) String content) {
why i need to add connection string when using blobtrigger?
in C# it is working without connection string:
public static void ProcessBlobContainer1([BlobTrigger("container1/{blobName}")] CloudBlockBlob blob, string blobName)
{
ProcessBlob("container1", blobName, blob);
}
i didn't see any Java sample for Azure functions for #BlobTrigger.
After all, connection is necessary for the trigger to identify where the container locates.
After test I find #Mikhail is right.
For C#, the default value(in local.settings.json or in application settings in portal) will be used if connection is ignored. But unfortunately there's no same settings for java.
You can add #StorageAccount("YourStorageConnection") below your #FuncionName as it's another valid way to choose. And value of YourStorageConnection in local.settings.json or in portal's application settings is up to you.
You can follow this tutorial, use mvn azure-functions:add to find four(Http/Blob/Queue/TimerTrigger) templates for java.

Incomplete java.net.URL.openStream() stream

I’m using java.net.URL.openStream() to access a HTTPS resource. The returned stream is incomplete for some URLs: for the example below, it yields a 1,105,724 byte-file whereas the same URL accessed from a browser yields a 5,755,858 byte-file (even when "disabling" Content-Encoding).
And it doesn’t even throw an exception.
What am I missing?
import static java.nio.file.Files.copy;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Paths;
public class Test {
public static void main(String... args) throws IOException {
try (final InputStream in = new URL(
"https://upload.wikimedia.org/wikipedia/commons/9/95/Germany_%28orthographic_projection%29.svg").openStream()) {
copy(in, Paths.get("germany.svg"));
}
}
}
Edit
I’ve tested this code a lot of times (on different networks, but always on JRE 1.8.0_60 / Mac OS X 10.11.4), and sometimes it’s suddenly "starting to work".
However, switching to another of my problematic URLs (e.g. "https://upload.wikimedia.org/wikipedia/commons/c/ce/Andorra_in_Europe_%28zoomed%29.svg") enables me to reproduce the issue.
Does this mean that it is a server issue? I’ve never seen it on a browser though.
It's working fine.
As others have suggested there may be a problem with your network, try connecting to another network.
package test;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class TestMain2 {
public static void main(String[] args) {
System.out.println("Started");
try (final InputStream in = new URL(
"https://upload.wikimedia.org/wikipedia/commons/9/95/Germany_%28orthographic_projection%29.svg")
.openStream()) {
Path outputFile = Paths.get("test.svg");
Files.copy(in, outputFile, StandardCopyOption.REPLACE_EXISTING);
System.out.println("Output file size : " + outputFile.toFile().length());
System.out.println("Finished");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Output
Started
Output file size : 5755858
Finished

streamscraper java lib eclipse

I have this code:
import java.net.URI;
import java.util.List;
import net.moraleboost.streamscraper.Stream;
import net.moraleboost.streamscraper.Scraper;
import net.moraleboost.streamscraper.scraper.IceCastScraper;
public class Harvester
{
public static void main(String[] args) throws Exception
{
Scraper scraper = new IceCastScraper();
List<Stream> streams = scraper.scrape(new URI("http://host:port/"));
for (Stream stream: streams) {
System.out.println("Song Title: " + stream.getCurrentSong());
System.out.println("URI: " + stream.getUri());
}
}
}
Where do I download JAR for import net.moraleboost.streamscraper.* to work? I can find source code for it but I gives me load of errors, can someone just provide me .jar so I could include in java build path library?
You can clone the repository at https://code.google.com/p/streamscraper/
You can also download the code from here: https://code.google.com/p/streamscraper/source/browse/

Categories