Results 1 to 12 of 12
  1. #1
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default 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

  2. #2
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    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:
    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);
    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:
    <FORM ENCTYPE="multipart/form-data" 
        method=post action="/myservlet"> 
      <INPUT TYPE="file" NAME="thefile">
      <INPUT TYPE="submit" VALUE="Upload">
    </FORM>
    Thanks

  3. #3
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    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 ;)

  4. #4
    Basit56 is offline Member
    Join Date
    Aug 2009
    Posts
    48
    Rep Power
    0

    Default

    Sorry i misunderstood the question.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by Basit56 View Post
    Sorry i misunderstood the question.
    The OP hasn't given enough information so it's not your fault.
    You could still have guessed correctly.

  6. #6
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    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.

  8. #8
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    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?

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    So read FON's reply again.

  10. #10
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    tnx.. problem solved :D

  11. #11
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    antrax would you be so kind to share that solution with rest
    of people on this forum

    thank you very much in advance :)

  12. #12
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    struts sample showcase contains fileupload method.. i look into the code and the destination directory was set in one of the xml file :) i just have to reuse it...

Similar Threads

  1. location provider returns no location
    By sandeeprao.techno in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-24-2009, 09:54 AM
  2. File Location
    By bcbird in forum New To Java
    Replies: 1
    Last Post: 09-17-2009, 08:03 AM
  3. Replies: 5
    Last Post: 07-26-2009, 03:37 PM
  4. Transferring a .txt file from 1 location to another
    By dbashby in forum New To Java
    Replies: 2
    Last Post: 04-15-2009, 05:22 AM
  5. Java logging - log file location for FileHandler
    By kfir.wolfson@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 03-24-2009, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •