0

我以前曾经工作过的代码,但现在开始失败,我的猜测可能是服务器上的某些内容发生了更改,可能是权限或其他内容。我需要弄清楚它可能是什么才能修复。

基本上我有:

  1. 将文件发送到服务器的 POST 请求
  2. 文件通过电子邮件作为附件发送

这是准备 Mimeparts 的代码

DiskFileItemFactory factory = new DiskFileItemFactory();        
ServletFileUpload upload = new ServletFileUpload(factory);

//parse request
List<?> items = upload.parseRequest(request);

HashMap<String, FileItem> form = new HashMap<String, FileItem>(); 

Iterator<?> iterator = items.iterator();
while (iterator.hasNext()) {
    FileItem item = (FileItem) iterator.next();           
    form.put(item.getFieldName(), item);
}


//handle attachment
if(form.containsKey("uploaded_file"))
{
    FileItem fileItem = form.get("uploaded_file");
    String fileName = fileItem.getName();
  File f = new File(fileName);
    try {
        InputStream inputStream = fileItem.getInputStream();

        OutputStream out=new FileOutputStream(f);
        byte buf[]=new byte[1024];
        int len;
        while((len=inputStream.read(buf))>0)
            out.write(buf,0,len);

        out.close();
        inputStream.close();

    }  catch (Exception e) {
        e.printStackTrace();
    }

    MimeBodyPart attachment = new MimeBodyPart();
    attachment.setDataHandler(new DataHandler(new FileDataSource(f)));
    attachment.setFileName(fileName);
}



//below I use a javax.mail.Transport to send it via email      
//mimeparts : new MimeBodyPart[]{attachment}

以前的代码可以正常工作,刚开始抛出这个异常:

    javax.mail.MessagingException:发送消息时出现IOException;
      嵌套异常是:
        java.io.FileNotFoundException:uploaded_file_name.zip(权限被拒绝)
        com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141)
        javax.mail.Transport.send0(Transport.java:195)
        javax.mail.Transport.send(Transport.java:124)

 
4

0 回答 0