Results 1 to 12 of 12
- 01-11-2010, 07:44 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 57
- Rep Power
- 0
how to specify location of file to be uploaded?
String htmlBody; // ...
byte[] attachmentData; // ...
Multipart mp = new MimeMultipart();
MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setContent(htmlBody, "text/html");
mp.addBodyPart(htmlPart);
MimeBodyPart attachment = new MimeBodyPart();
attachment.setFileName("manual.pdf");
attachment.setContent(attachmentData, "application/pdf");
mp.addBodyPart(attachment);
message.setContent(mp);
lets say manual.pdf is in C:/files folder
- 01-11-2010, 08:19 AM #2
Member
- Join Date
- Aug 2009
- Posts
- 48
- Rep Power
- 0
Hi,
May be this will help you.
Sending Attachments
Sending attachments is quite like forwarding messages. You build up the parts to make the complete message. After the first part, your message text, you add other parts where the DataHandler for each is your attachment, instead of the shared handler in the case of a forwarded message. If you are reading the attachment from a file, your attachment data source is a FileataSource. Reading from a URL, it is a URLDataSource. Once you have your DataSource, just pass it on to the DataHandler constructor, before finally attaching it to the BodyPart with setDataHandler(). Assuming you want to retain the original filename for the attachment, the last thing to do is to set the filename associated with the attachment with the setFileName() method of BodyPart. All this is shown here:
When including attachments with your messages, if your program is a servlet, your users must upload the attachment besides tell you where to send the message. Uploading each file can be handled with a form encoding type of multipart/form-data.Java Code:// Define message Message message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); message.setSubject("Hello JavaMail Attachment"); // Create the message part BodyPart messageBodyPart = new MimeBodyPart(); // Fill the message messageBodyPart.setText("Pardon Ideas"); Multipart multipart = new MimeMultipart(); multipart.addBodyPart(messageBodyPart); // Part two is attachment messageBodyPart = new MimeBodyPart(); DataSource source = new FileDataSource(filename); messageBodyPart.setDataHandler(new DataHandler(source)); messageBodyPart.setFileName(filename); multipart.addBodyPart(messageBodyPart); // Put parts in message message.setContent(multipart); // Send the message Transport.send(message);
ThanksJava Code:<FORM ENCTYPE="multipart/form-data" method=post action="/myservlet"> <INPUT TYPE="file" NAME="thefile"> <INPUT TYPE="submit" VALUE="Upload"> </FORM>
- 01-11-2010, 12:10 PM #3
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
It's not clear what you want to do.
You asked about file upload, and you posted code for sending e-mail with attach...or...?
So Basit56 gave you good place to start with: upload form.
This way user can easily pick any file from his file-system and
send it to your server. Your job is here to create a server component
that will receive file and store it on servers file system.
This is not standard thing in servlet.api so you have to use
additional libraries for this.
You can choose Apache Jakarta Commons Upload:
FileUpload - Using FileUpload
Read that page and you will find exactly how to
specify location of file.
Pay attention on file system permissions and unique file names on server.
good luck ;)
- 01-11-2010, 01:00 PM #4
Member
- Join Date
- Aug 2009
- Posts
- 48
- Rep Power
- 0
Sorry i misunderstood the question.
- 01-11-2010, 01:06 PM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
- 01-11-2010, 01:16 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 57
- Rep Power
- 0
tnx a lot for the answers.. it also has helped me alot..
i have another question
can i include files with include tag other than text/html?
<%@ include file="/files/Struts2.pdf" %> i did this but i get this error
same with .doc type.. is there other method to display .pdf or .doc to html?
SEVERE: Servlet.service() for servlet jsp threw exception
org.apache.jasper.JasperException: /files/Struts2.pdf(8,327) #{..} is not allowed in template text
at org.apache.jasper.compiler.DefaultErrorHandler.jsp Error(DefaultErrorHandler.java:40)
.
.
.Last edited by anthrax; 01-11-2010 at 01:19 PM.
- 01-11-2010, 01:22 PM #7
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
You should generally start a new thread for a new question.
You didn't say whether you were uploading files to a server or emailing attachments.
You should run a file servlet for your current problem but I'll not go into details because you are not willing to give enough information to people who are trying to help you for free.
- 01-11-2010, 01:43 PM #8
Member
- Join Date
- Feb 2009
- Posts
- 57
- Rep Power
- 0
i'm actually uploading file to a server and i want it be placed to a specific folder and change its filename to my liking.. when i found the code which i 1st posted i didn't realize it was meant for attaching file to email :D.. but Basit56 was able to explain it well and i was able to understand the process for attaching file to email.. now i'm back with uploading file from the server with this html form
<FORM ENCTYPE="multipart/form-data"
method=post action="/myservlet">
<INPUT TYPE="file" NAME="thefile">
<INPUT TYPE="submit" VALUE="Upload">
</FORM>
for example i want the uploaded file be placed in C:/file and its filename be saved as a.pdf .. also where is this "fileSystemPath" located?
- 01-11-2010, 01:47 PM #9
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
So read FON's reply again.
- 01-11-2010, 02:04 PM #10
Member
- Join Date
- Feb 2009
- Posts
- 57
- Rep Power
- 0
tnx.. problem solved :D
- 01-11-2010, 02:18 PM #11
Senior Member
- Join Date
- Dec 2009
- Location
- Belgrade, Serbia
- Posts
- 364
- Rep Power
- 4
antrax would you be so kind to share that solution with rest
of people on this forum
thank you very much in advance :)
- 01-13-2010, 02:48 PM #12
Member
- Join Date
- Feb 2009
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
location provider returns no location
By sandeeprao.techno in forum CLDC and MIDPReplies: 0Last Post: 09-24-2009, 09:54 AM -
File Location
By bcbird in forum New To JavaReplies: 1Last Post: 09-17-2009, 08:03 AM -
How to write the JNLP appln for installing the note.exe file in the client location
By srilatha in forum Advanced JavaReplies: 5Last Post: 07-26-2009, 03:37 PM -
Transferring a .txt file from 1 location to another
By dbashby in forum New To JavaReplies: 2Last Post: 04-15-2009, 05:22 AM -
Java logging - log file location for FileHandler
By kfir.wolfson@gmail.com in forum Advanced JavaReplies: 2Last Post: 03-24-2009, 08:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks