Thursday, February 12, 2004

File upload / email via Java


Here are some notes on something I had to do today. There are some resources out there on the web which I have used to do it.
The things I needed to do were:
1. write an HTML form which would allow you to add a file to the form which would be sent to the server on Submit
2. write the server-side code to get the uploaded file
3. take the uploaded file and email it somewhere as an attachment.

OK - here's the HTML form (check the source!):






There are various other things you can specify on the form e.g. a max size.

On the server side, here are the basics of the code, with all the extraneous stuff taken out.
Create a MultipartRequest (in com.oreilly.servlet.*), get the file,
then create a multi-part MIME message to send it...


First you have to create a MultipartRequest, a convenience class provided from Jason Hunter's Servlets.com site:


File f = null;
MultipartRequest mprequest = new MultipartRequest(request,"C:\\upload");
Enumeration files = mprequest.getFileNames();
while (files.hasMoreElements()) {
String fileName = (String)files.nextElement();
f = mprequest.getFile(fileName);
}



Then create the mail properties - example follows:



Properties props = new Properties();
props.put("mail.smtp.host", "10.248.2.2");

java.util.Date today = new java.util.Date();
int m = today.getMonth()+1;
int y = today.getYear()-100;
String tDay = "["+today.getDate()+"/"+m+"/0"+y+"] ";
String remSummary = "["+country+"] "+tDay+summary;



We need a session object... not quite sure why at the moment...




Session s = Session.getDefaultInstance(props,null);
Session s2 = Session.getDefaultInstance(props,null);



Then create the messages themselves.
Note that the setRecipients method expects an array of Addresses.
Then you have to create a message with multiple parts for the body and attachment. The way to do it is below... I haven't understood it in great depth but anyway... note the data source handling for the attachment types...




Message msg = new MimeMessage(s);
InternetAddress fromAddr = new InternetAddress(email);
msg.setFrom(fromAddr);
InternetAddress[] toAddr = new InternetAddress[]{new InternetAddress("sendto@my.com")};
msg.setRecipients(Message.RecipientType.TO,toAddr);
msg.setSubject("Request: " + summary);
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(description, "text/plain");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);

MimeBodyPart file_part = new MimeBodyPart();
FileDataSource fds = new FileDataSource(f);
DataHandler dh = new DataHandler(fds);
file_part.setFileName(f.getName());
file_part.setDisposition(Part.ATTACHMENT);
file_part.setDescription("Attached file: " + f.getName());
file_part.setDataHandler(dh);
multipart.addBodyPart(file_part);

msg.setContent(multipart);
Transport.send(msg);


MimeMessage message2 = new MimeMessage(s2);
message2.setContent(htmlText, "text/html");
InternetAddress from2 = new InternetAddress("sendto@my.com");
message2.setFrom(from2);
InternetAddress to2 = new InternetAddress(email);
message2.addRecipient(Message.RecipientType.TO, to2);
message2.setSubject("Confirmation of Your Request");

Transport.send(message2);

Monday, February 09, 2004

PuTTY options



You can change them directly in the registry rather than laboriously going thru each session in the GUI...
regedit the following: HKEY_CURRENT_USER\Software\SimonTatham\ etc.

Eclipse FTP support


Can't remember if I've taken any notes about this before. I seem to remember trying to get it working, but not achieving very much. I am now trying again so here are the notes...

1. The download files for Eclipse 2.1.2 FTP/WebDAV support is here.
2. Extract the file structure to your Eclipse directory.
3. Restart Eclipse - I think it will prompt you about the pending configuration change to install FTP/WebDAV
4. Click Window -> Show View -> Other...
5. Click "Site Explorer" under "Target Management"
6. In Site Explorer, rightclick and New -> Target Site
7. Choose an FTP connection
8. Fill in the FTP connection details. "Passive mode" seemed to work better for me on the site I tried initially, but I don't actually know what that means... read this for some info on Active vs. Passive FTP...
9. Download the files as a Project etc... it's pretty slow though... F5 for Refresh of the directories should work OK.

This allows you to download stuff into your local Eclipse workspace. How to conveniently save resources back to the remote server via FTP though? At the moment my projects are connected to CVS so I can only upload them by doing a CVS commit... Have played with this a bit and you seem to have to do the following:
1. right click on the project you are trying to muck around with
2. Click Team -> Target Site... (be careful to link the project to the right remote directory!!... I got this wrong at first)
3. This should allow you to choose or create an FTP location to upload to
4. Now right click the resource you want to upload (does this work for whole directories, recursively, too? Don't know yet...) and click Team -> Upload

Am still playing with this... it seems to have some idiosyncrasies so check that your resources are actually getting where you think they should be... there are some FTP tracing options you can put on if it's looking weird - the info below is from http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.team.ftp/doc-html/faq.html?rev=1.5

To test whether this issue is what is giving you a problem, you can turn on FTP debugging in Eclipse.

Create a file named ".options" in your eclipse executable directory
Put the following lines in the file:
org.eclipse.team.ftp/debug=true
org.eclipse.team.ftp/requests=true
org.eclipse.team.ftp/responses=true
org.eclipse.team.ftp/list=true
Start eclipse with the extra parameters "-vm /path/to/java.exe -consolelog -debug" (se java.exe instead of javaw.exe). This will dump the low level ftp output to the console.



Maybe MyEclipse has a better way of doing FTP than the free Eclipse tools... ??

NJoy...

Labels: