请问高手如何处理邮件附件?我用JSP的setHeader方法来下载,可是文本文件时有问题啊?

Tarloy 2001-10-18 12:42:14
==========================================================================String fileContentType=(String)hs.get("contentType");
InputStream in=(InputStream)hs.get("inputStream");
String attachmentName=(String)hs.get("attachmentName");

if(in!=null){
response.setContentType(fileContentType);
response.setHeader("Content-Disposition","attachment; filename=\"" +attachmentName+ "\"");
OutputStream outputStream = response.getOutputStream();
int c = in.read();
while(c != -1)
{
outputStream.write(c);
c = in.read();
}
}
==========================================================================
如果是文本文件时会出现找不到文件的错误。
谁能告诉我为什么会这样。

有没有更好的解决方法,最好有原代码.
Thinks in advance.
...全文
130 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
skyyoung 2001-10-29
  • 打赏
  • 举报
回复
http://www.csdn.net/expert/TopicView.asp?id=83102
Tarloy 2001-10-29
  • 打赏
  • 举报
回复
to  hicharlie(黑查理):

我给你加了,谢谢你给了我提醒.

hicharlie 2001-10-19
  • 打赏
  • 举报
回复
这个问题先结了吧!
中文的文件名处理要想一些办法,比如说,把文件名放在form中等。
Tarloy 2001-10-19
  • 打赏
  • 举报
回复
搞定,我忘了close()了,但中文文件名时显示还是不正常,

高手啊,快来斑斑我啊!
hicharlie 2001-10-18
  • 打赏
  • 举报
回复
敝人写的一个让浏览器下载文件的类的代码片断,看看合不合用。
/**下载一个文件
* @param sourceFilePathName 源文件路径
* @param contentType Content类型
* @param destFileName 目标文件
* @param blockSize 源文件
*功能:将一个文件数据发送给客户端下载
*/
public void downloadFile(String sourceFilePathName, String contentType, String destFileName, int blockSize)
throws JPowerUploadException, IOException, ServletException
{
if(sourceFilePathName == null)
throw new IllegalArgumentException(String.valueOf((new StringBuffer("File '")).append(sourceFilePathName).append("' not found (1040).")));
if(sourceFilePathName.equals(""))
throw new IllegalArgumentException(String.valueOf((new StringBuffer("File '")).append(sourceFilePathName).append("' not found (1040).")));
if(!isVirtual(sourceFilePathName) && m_denyPhysicalPath)
throw new SecurityException("Physical path is denied (1035).");
if(isVirtual(sourceFilePathName))
sourceFilePathName = m_application.getRealPath(sourceFilePathName);
java.io.File file = new java.io.File(sourceFilePathName);
FileInputStream fileIn = new FileInputStream(file);
long fileLen = file.length();
int readBytes = 0;
int totalRead = 0;
byte b[] = new byte[blockSize];
if(contentType == null)
m_response.setContentType("application/x-msdownload");
else
if(contentType.length() == 0)
m_response.setContentType("application/x-msdownload");
else
m_response.setContentType(contentType);
m_response.setContentLength((int)fileLen);
m_contentDisposition = m_contentDisposition != null ? m_contentDisposition : "attachment;";
if(destFileName == null)
m_response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append(getFileName(sourceFilePathName))));
else
if(destFileName.length() == 0)
m_response.setHeader("Content-Disposition", m_contentDisposition);
else
m_response.setHeader("Content-Disposition", String.valueOf((new StringBuffer(String.valueOf(m_contentDisposition))).append(" filename=").append(destFileName)));
while((long)totalRead < fileLen)
{
readBytes = fileIn.read(b, 0, blockSize);
totalRead += readBytes;
m_response.getOutputStream().write(b, 0, readBytes);
}

fileIn.close();
}
piggyqiu 2001-10-18
  • 打赏
  • 举报
回复
关注...
我以前类似的这样做过下载...不会找不到啊...
不过就是在不同的浏览器下...ie5.0 和 ie5.5 的反应好象不一样...

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧