Salvatorelab
Salvatorelab

Reputation: 11873

Easiest way to send files to a server

I have a Java desktop application that can generate logs. I would like to send those logs to a remote server.

What is the correct way of doing that? via FTP? writing a small java server and sending it with a socket? posting the contents to a PHP form?

If using FTP, is it secure? I mean, is it possible to allow uploads but also protect files from being deleted or renamed?

Upvotes: 1

Views: 813

Answers (4)

Salvatorelab
Salvatorelab

Reputation: 11873

Thanks to @VarunAchar I've decided to send the logs via email. But instead of using log4j (it is a good idea to use it, but in my case I do not need such a complete solution) I've used my own method to send an email with some attachments:

Properties properties = new Properties();
properties.setProperty("mail.smtp.submitter", from); //such as [email protected]
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.host", host);   //use smtp.gmail.com for Google
properties.put("mail.smtp.user", from); //such as [email protected]
properties.put("mail.smtp.port", port); //use 465 for Google's SMTP server
properties.put("mail.smtp.socketFactory.port", port); //use 465 for Google's SMTP server
properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.socketFactory.fallback", "false");

Authenticator mailAuthenticator = new MailAuthenticator(from,"mypassword");
Session mailSession = Session.getDefaultInstance(properties,mailAuthenticator);
Message message = new MimeMessage(mailSession);

InternetAddress fromAddress = new InternetAddress(from);
InternetAddress toAddress = new InternetAddress(to);

message.setFrom(fromAddress);
message.setRecipient(RecipientType.TO, toAddress);
//you can use RecipientTypes such as
//RecipientType.TO
//RecipientType.BCC
//RecipientType.CC
//RecipientType.NEWSGROUPS
message.setSubject(subject);

// Create the message part 
BodyPart messageBodyPart = new MimeBodyPart();

// Some text
messageBodyPart.setText(text);

// Attachments
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Attach error.log
File f = new File(ruta+"error.log");
if (f.exists()) {
    messageBodyPart = new MimeBodyPart();
    String filename = ruta+"error.log";
    DataSource source = new FileDataSource(filename);
    messageBodyPart.setDataHandler(new DataHandler(source));
    messageBodyPart.setFileName(filename);
    multipart.addBodyPart(messageBodyPart);
}

// repeat with more attachments if needed

// Fill email content
message.setContent(multipart);

Transport.send(message);

It uses Java Mail API
The original code was found here: http://tunatore.wordpress.com/2011/10/05/how-to-send-an-e-mail-using-java-mail-api-swing-desktop-application-using-googles-smtp-example/
And the attachment part was here: http://www.roseindia.net/javamail/SendAttachment.shtml

Upvotes: 0

Pradip Bhatt
Pradip Bhatt

Reputation: 658

Yes... Log4J is very useful for logging mechanism in your application.

And for sending data to the server, its become very easy using client socket - server socket.

There are very useful methods for sending your data from client to server and vice versa.

Upvotes: 0

Varun Achar
Varun Achar

Reputation: 15109

Emailing the log file is also another option. This gives you the option of sending the log to a list of people right on their emails in case you need to perform an action immediately based on the error.

For log4j check this link:

http://www.codereye.com/2009/02/sending-email-alerts-with-log4j.html

Basic gist is is :

log4j.rootLogger=INFO, a, email
log4j.appender.a=org.apache.log4j.ConsoleAppender
log4j.appender.a.layout=org.apache.log4j.PatternLayout
log4j.appender.a.layout.ConversionPattern=%d{HH:mm:ss} %-5p [%c{1}]: %m%n
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.BufferSize=10
log4j.appender.email.SMTPHost=mysmtp.mailserver.net
[email protected]
[email protected]
log4j.appender.email.Subject=My Module Error
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n

Also check the AsynAppender for sending emails asynchronously so that current thread isn't blocked for the email to be sent.

Upvotes: 1

n1ckolas
n1ckolas

Reputation: 4450

In your particular case I would recommend using log4j's SimpleSocketServer.

In your server you can just start server with the following parameters:

java -cp log4j.jar org.apache.log4j.net.SimpleSocketServer 4712

Here is sample log4j.properties for the server:

log4j.rootLogger=debug, stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

Here is sample log4j.properties for client:

log4j.appender.SERVER=org.apache.log4j.net.SocketAppender
log4j.appender.SERVER.Port=4712
log4j.appender.SERVER.RemoteHost=loghost
log4j.appender.SERVER.ReconnectionDelay=10000

The only note, is that you can't set some sort of authentication with capabilities of SimpleSocketServer only. You can only use some other ways to achieve that, f.e. SSH-tunnel.

Upvotes: 1

Related Questions