webtier@glassfish.java.net

Re: JavaMail 1.4.4/demo/msgshow.java

From: John Yeary <johnyeary_at_gmail.com>
Date: Mon, 19 Sep 2011 09:31:55 -0400

Hello Jose,

Please check out this site http://techhelp.santovec.us/decode.htm It has
details on the various encoding types and determining file names. It is
likely you are interested in the Content-Type

John

On Sat, Sep 17, 2011 at 3:26 AM, <forums_at_java.net> wrote:

> Hi,
>
> I have resolved all my problems. This is the method as it leaves,
>
> private void dumpPart(
> Part p,
> int attnum,
> HttpServletRequest request,
> HttpServletResponse response)
> throws Exception {
>
> try {
>
> HTMLMessage htmlMessage = new HTMLMessage();
> List<DataFile> dfList = new ArrayList<DataFile>();
>
> if (p instanceof Message)
> htmlMessage = dumpEnvelope((Message)p);
>
> /** Dump input stream ..
>
> InputStream is = p.getInputStream();
> // If "is" is not already buffered, wrap a
> BufferedInputStream
> // around it.
> if (!(is instanceof BufferedInputStream))
> is = new BufferedInputStream(is);
> int c;
> while ((c = is.read()) != -1)
> System.out.write(c);
>
> **/
>
> /*
> * Using isMimeType to determine the content type
> avoids
> * fetching the actual content data until we need it.
> */
> if (p.isMimeType("text/plain")) {
> String content = (String)p.getContent();
> htmlMessage.setMessage(**content.getBytes());
> } else if (p.isMimeType("multipart/*")) {
> Multipart mp = (Multipart)p.getContent();
> int count = mp.getCount();
> for (int i = 0; i < count; i++){
> dumpPart(mp.getBodyPart(i), ++attnum,
> request, response);
> }
> } else if (p.isMimeType("message/rfc822"**)) {
> dumpPart((Part)p.getContent(), ++attnum, request,
> response);
> } else {
> /*
> * If we actually want to see the data, and it's
> not a
> * MIME type we know, fetch it and check its
> Java type.
> */
> Object o = p.getContent();
> if (o instanceof String) {
>
> htmlMessage.setMessage(((**String)o).getBytes());
> } else if (o instanceof InputStream) {
> InputStream is = (InputStream)o;
> int c;
> StringBuffer sb = new StringBuffer();
> while ((c = is.read()) != -1)
> sb.append(c);
> byte[] content =
> (sb.toString()).getBytes();
> htmlMessage.setMessage(**content);
> } else {
> ByteArrayOutputStream byteOut = new
> ByteArrayOutputStream();
> ObjectOutputStream objOut = new
> ObjectOutputStream(byteOut);
> objOut.writeObject(o);
> byte[] b = byteOut.toByteArray();
> htmlMessage.setMessage(b);
> }
> }
>
> /*
> * If we're saving attachments, write out anything that
> * looks like an attachment into an appropriately named
> * file. Don't overwrite existing files to prevent
> * mistakes.
> */
>
> String strCt = p.getContentType();
> ContentType ct = new ContentType(strCt);
> String bt = ct.getBaseType();
>
> String filename = p.getFileName();
>
> if (p.isMimeType("multipart/*")) {
> String disp = p.getDisposition();
> // many mailers don't include a
> Content-Disposition
> if (disp == null ||
> disp.equalsIgnoreCase(Part.**ATTACHMENT)) {
> if (filename == null)
> filename = "Attachment" + attnum;
>
> DataFile df = new DataFile();
>
> File f = new File(filename);
> if (f.exists())
> // XXX - could try a series of names
> throw new IOException("file
> exists");
>
> df.setContentType(bt);
> df.setFileName(filename);
> df.setFile(f);
> //((MimeBodyPart)p).saveFile(**f);
>
> dfList.add(df);
> }
> }
>
> HashMap<HTMLMessage, List<DataFile>> hashMap = new
> HashMap<HTMLMessage, List<DataFile>>();
> hashMap.put(htmlMessage, dfList);
>
> save(hashMap);
>
> } catch (Exception e) {
> request.setAttribute("**exception", e);
>
> request.getRequestDispatcher("**/error.jsp").forward(request, response);
> }
>
> }
> public void save(HashMap<HTMLMessage, List<DataFile>> hashMap){
> messagesAttachedFileManager.**placeMessagesAttachedFile(**
> hashMap);
> }
>
>
> being 'messagesAttachedFileManager' an ejb facade.
>
> Everything works fine. Now I have another problem, I do not know the way to
> get the extension of the files,
>
> String filename = p.getFileName();
>
> if (disp == null ||
> disp.equalsIgnoreCase(Part.**ATTACHMENT)) {
> if (filename == null)
> filename = "Attachment" + attnum;
>
> Well, I put a name to the files but without extension.
>
> How could I know witch are those extensions?
>
> Kind regards,
> Jose
>
>
> --
>
> [Message sent by forum member 'josealvarezdelara']
>
> View Post: http://forums.java.net/node/**843703<http://forums.java.net/node/843703>
>
>
>


-- 
John Yeary
--
http://javaevangelist.blogspot.com
http://www.johnyeary.com
*_at_jyeary*
"Far better it is to dare mighty things, to win glorious triumphs, even
though checkered by failure, than to take rank with those poor spirits who
neither enjoy much nor suffer much, because they live in the gray twilight
that knows not victory nor defeat."
-- Theodore Roosevelt