使用电子邮件附件的Tomcat文件上载失败 - 权限被拒绝

时间:2013-03-07 21:10:05

标签: java file tomcat upload permissions

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

基本上我有:

  1. 将文件发送到服务器的POST请求
  2. 文件通过电子邮件发送为附件
  3. 以下是准备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 while sending message;
          nested exception is:
            java.io.FileNotFoundException: uploaded_file_name.zip (Permission denied)
            com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1141)
            javax.mail.Transport.send0(Transport.java:195)
            javax.mail.Transport.send(Transport.java:124)
    
     

0 个答案:

没有答案