Posted By:
James_Kelley
Posted On:
Wednesday, December 5, 2001 03:57 PM
I am having problems decode attachments that are not base64, but are 7 bit 8 bit, ... What am I doing wrong? Do I have to modify my code (below)to use InputStream in=mbp.getInputStream(); String[] encoding=mbp.getHeader("Content-Transfer-Encoding"); if(encoding!=null && encoding.length>0){ in=MimeUtility.decode(in,encoding[0]); } or should writing the input stream to the out do an automatic decode for me? Thanks for the help my code String fileName = p.getFileName(); String disposition = p.getDisposition(); String contentType = p.getContentType(); String encoding = p.getEncoding();
More>>
I am having problems decode attachments that are not base64, but are 7 bit 8 bit, ... What am I doing wrong? Do I have to modify my code (below)to use
InputStream in=mbp.getInputStream();
String[] encoding=mbp.getHeader("Content-Transfer-Encoding");
if(encoding!=null && encoding.length>0){
in=MimeUtility.decode(in,encoding[0]);
}
or should writing the input stream to the out do an automatic decode for me?
Thanks for the help
my code
String fileName = p.getFileName();
String disposition = p.getDisposition();
String contentType = p.getContentType();
String encoding = p.getEncoding();
File attachmentDir = new File(attachmentDirStr);
if(!attachmentDir.exists())
attachmentDir.mkdirs();
if (fileName == null && (disposition.equals(Part.ATTACHMENT) || !contentType.equals("text/plain")))
{
// pick a random file name. This requires Java 1.2 or later.
fileName = File.createTempFile("attachment", ".txt",attachmentDir).getName();
//we need this to return a file name
newName = fileName.toString();
}
if (fileName == null)
{ // likely inline
p.writeTo(System.out);
}
else
{
File f = new File(attachmentDir,fileName);
int i = 0;
// find a version that does not yet exist attachment directory
newName = fileName;
while (f.exists())
{
i++;
newName = i + fileName;
f = new File(attachmentDir,newName);
}
FileOutputStream out = new FileOutputStream(f);
// We can't just use p.writeTo() here because it doesn't
// decode the attachment. Instead we copy the input stream
// onto the output stream which does automatically decode
// Base-64, quoted printable, and a variety of other formats.
InputStream in = new BufferedInputStream(p.getInputStream());
int b;
while ((b = in.read()) != -1)
{
out.write(b);
}
out.flush();
out.close();
in.close();
}
<<Less