ZKUploader to sendspace"

From Documentation
m
Line 1: Line 1:
 
{{Template:Smalltalk_Author|
 
{{Template:Smalltalk_Author|
 
|author=Diogo Duarte, Engineer, SCOPROLUMBA Corporation
 
|author=Diogo Duarte, Engineer, SCOPROLUMBA Corporation
 +
<br />
 +
<div>
 +
<div style="border:solid 1px; padding:5px; height:100px;width:600px" >
 +
<div style="float:left;padding-bottom:15px;padding-right:10px;border:0 none;vertical-align:middle;">[[Image:diogoduarte.jpg]]</div>Diogo is the Information Systems Director at Scoprolumba. He has a degree in Electrical Engineering and Computer Science and develops using Java, Coldfusion and Delphi. Nowadays more Java and Delphi.
 +
</div>
 +
<br />
 
|date=November 18, 2010
 
|date=November 18, 2010
 
|version=ZK 5.0.5
 
|version=ZK 5.0.5

Revision as of 07:51, 23 November 2010

ZKUploader to sendspace

Author
Diogo Duarte, Engineer, SCOPROLUMBA Corporation


Diogoduarte.jpg
Diogo is the Information Systems Director at Scoprolumba. He has a degree in Electrical Engineering and Computer Science and develops using Java, Coldfusion and Delphi. Nowadays more Java and Delphi.

Date
November 18, 2010
Version
ZK 5.0.5

Introduction

In my company it's often needed to send large files (blueprints and other media) to several supplier for price quotations. The use of an FTP server has several issues, bandwidth restriction, security problems ... The best way i've found to solve this problem was an app that takes the file/s and uploads to a file share server. After the file is uploaded, the user will be notified by email containing a link to the file. I chose sendspace becouse it has a java API that enables free anonymous upload. the first thing to do tought is to get and API key in the site sendspace.com/developers.

Live Demo

The index.zul

<?page title="ZK Sender - SendSpace"?>
<window title="ZK Sender - SendSpace" border="normal" width="280px" apply="dd.sendspace.SendSpaceWindowComposer">
		<vbox>
			<menubar>
				<menu label="SendSpace">
					<menupopup>
						<menuitem label="Ask for an API key" href="http://www.sendspace.com/dev_apikeys.html"/>
						<menuseparator />
						<menuitem label="API Methods" href="http://www.sendspace.com/dev_method.html"/>
					</menupopup>
				</menu>
				<menuitem label="Send Large Files" upload="true"
					image="/img/PaperClip-16x16.png" id="DDUploader"/>
			</menubar>
			<label value="Please enter your email:" />
			<textbox id="txEmail" hflex="true" constraint="no empty:you have to place the email here"/>
		</vbox>
</window>

The SendSpaceWindowComposer.java

package dd.sendspace;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.zkoss.util.media.Media;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.Textbox;

import sendspaceapi.Data.AnonymousFileEntry;
import sendspaceapi.Upload.Anonymous.AnonymousUpload;

public class SendSpaceWindowComposer extends GenericForwardComposer {

	/**
	 * SendSpace ZK Uploader
	 * Send and go home :)
	 *
	 * @author DiogoDuarte
	 */
	private static final long serialVersionUID = -4533393095539134635L;
	// put you API key here
	private static String key = "YOUHAVETOPUTYOURKEYHERE";
	private Menuitem DDUploader;
	private Textbox txEmail;


	public void onBlur$txEmail(Event event) {
		if (txEmail.isValid()) {
			DDUploader.setDisabled(false);
		} else {
			DDUploader.setDisabled(true);
		}
	}

	/**
	 * Thread that forks the process to avoid waiting.
	 * 
	 */
	public class StartUpload extends Thread {
		String FileName1;

		public void FileName(String name) {
			FileName1 = name;
			start();
		}
		public void run() {
			String[] args1;
			args1 = new String[1];
			args1[0] = FileName1;
			send(args1);
			File f = new File(FileName1);
			f.delete();
		}
	}

	/**
	 * Fires when the user selects file for upload big files to upload to
	 * sendspace
	 * 
	 */
	public void onUpload$DDUploader(UploadEvent event) {
		Media media = event.getMedia();
		String f1 = saveFile(media);
		StartUpload Su1 = new StartUpload();
		Su1.FileName(f1);
	}

	/**
	 * Sends email to user
	 * 
	 */
	public static void sendEmailToUser(AnonymousFileEntry e,String toMail) {	
		String from = "SENDSPACE@gmail.com";
		String to = toMail;
		String subject = "FICHEIRO ENVIADO PARA SENDSPACE.COM";
		String message = "\n" 
				+ "Nome do ficheiro: " + e.fileName() + "\n\n" 
				+ "Links: \n" + "Link para Download: " + e.downloadURL() + "\n" 
				+ "\n" + "Link para Apagar: "
				+ e.deleteURL() + "\n\n"
				+ "Basta enviar o link para download.\n O link para apagar deve ser retirado!";

		SendMail sendMail = new SendMail(from, to, subject, message);
		sendMail.send();
	}

	/**
	 * Saves the file locally on the server.
	 * 
	 */
	@SuppressWarnings("finally")
	private String saveFile(Media media) {
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		String SAVE_PATH = Sessions.getCurrent().getWebApp().getRealPath(
				"/files")
				+ "\\";
		try {
			InputStream fin = media.getStreamData();
			in = new BufferedInputStream(fin);
			File baseDir = new File(SAVE_PATH);

			if (!baseDir.exists()) {
				baseDir.mkdirs();
			}

			File file = new File(SAVE_PATH + media.getName());

			OutputStream fout = new FileOutputStream(file);
			out = new BufferedOutputStream(fout);
			byte buffer[] = new byte[1024];
			int ch = in.read(buffer);
			while (ch != -1) {
				out.write(buffer, 0, ch);
				ch = in.read(buffer);
			}
			System.out.println("sucessed upload :" + media.getName());
		} catch (IOException e) {
			throw new RuntimeException(e);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			try {
				if (out != null)
					out.close();

				if (in != null)
					in.close();
				return SAVE_PATH + media.getName();

			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

	}

	/**
	 * Send the file
	 * 
	 */
	public void send(String[] args) {
		try {
			AnonymousUpload au = new AnonymousUpload(key);
			System.out.println("uploading file(s)...");
			if (args.length <= 0) {
				System.out.println("No files to upload!");
				return;
			} else if (args.length == 1) {
				AnonymousFileEntry e = au.uploadFile(new File(args[0]));
				sendEmailToUser(e,txEmail.getValue());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static File[] createFileList(String[] files) {
		File[] f = new File[files.length];
		for (int i = 0; i < files.length; i++) {
			f[i] = new File(files[i]);
		}
		return f;
	}

	public SendSpaceWindowComposer() {
	}

	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
	}
}

The SendMail.java

Just check your mailserver settings and alter/add.

package dd.sendspace;


import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendMail {

	private String from;
	private String to;
	private String subject;
	private String text;
	
	public SendMail(String from, String to, String subject, String text){
		this.from = from;
		this.to = to;
		this.subject = subject;
		this.text = text;
	}
	
	public void send(){
		
		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com"); // Usually your smtp.server
		props.put("mail.smtp.port", "465");// Usually 25

		Session mailSession = Session.getDefaultInstance(props);
		Message simpleMessage = new MimeMessage(mailSession);
		
		InternetAddress fromAddress = null;
		InternetAddress toAddress = null;
		try {
			fromAddress = new InternetAddress(from);
			toAddress = new InternetAddress(to);
		} catch (AddressException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			simpleMessage.setFrom(fromAddress);
			simpleMessage.setRecipient(RecipientType.TO, toAddress);
			simpleMessage.setSubject(subject);
			simpleMessage.setText(text);
			
			Transport.send(simpleMessage);			
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}

Downloads

Comments



Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.