我正在尝试创建一个可以处理附件的简单javamail应用程序,但每当我发送电子邮件时它都会显示“java.io.FileNotFoundException:fileName”。上传我已经验证的文件没有问题,但它无法提取上传文件的绝对路径。我使用html表单发送多部分表单数据。以下是我的java代码:
将帖子MailApp.java:
@WebServlet("/EmailSendingServlet")
@MultipartConfig(fileSizeThreshold=58576, maxFileSize=20848820, maxRequestSize=418018841)
public class MailApp extends HttpServlet {
private String host;
private String port;
private static final String SAVE_DIR = "uploadFiles";
public void init() {
host = "smtp.gmail.com";
port = "587"; }
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String recipient = null;
String subject = null;
String content = null;
String user = null;
String pass = null;
String fileName = null;
// gets absolute path of the web application
String appPath = request.getServletContext().getRealPath("");
// constructs path of the directory to save uploaded file
String savePath = appPath + File.separator + SAVE_DIR;
// creates the save directory if it does not exists
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
for( Part p : request.getParts() ) {
if( "uploaded_file".equals(p.getName()) ) {
fileName = extractFileName(p);
// refines the fileName in case it is an absolute path
fileName = new File(fileName).getName();
p.write(savePath + File.separator + fileName);
}
else if( "recipient".equals(p.getName()) ) {recipient = request.getParameter("recipient");}
else if( "subject".equals(p.getName()) ) {subject = request.getParameter("subject");}
if( "content".equals(p.getName()) ) {content = request.getParameter("content");}
else if( "user".equals(p.getName()) ) {user = request.getParameter("user");}
else if( "pass".equals(p.getName()) ) {pass =request.getParameter("pass");}
}
String resultMessage = "";
try {
SendMail.sendEmail(host, port, user, pass, recipient, subject, content, fileName);
resultMessage = "The e-mail was sent successfully";
} catch (Exception ex) {
ex.printStackTrace();
resultMessage = "There were an error: " + ex.getMessage();
}
finally {
request.setAttribute("Message", resultMessage);
getServletContext().getRequestDispatcher("/iml.jsp").forward(
request, response);
}
}
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("filename")) {
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}}
SendMail.java:
public class SendMail
{
public static void sendEmail(String host, String port,
final String userName, final String password, String toAddress,
String subject, String message,String fileName) throws AddressException,
MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileName);
multipart.addBodyPart(messageBodyPart);
// sets the multi-part as e-mail's content
msg.setContent(multipart);
try{ // sends the e-mail
Transport.send(msg);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
请帮我解决它
答案 0 :(得分:0)
您需要创建一个目录来保存上传的文件
您需要为文件
您需要使用以下方法将上传到您保存的文件的文件写入:
filePart.write(FULL_FILE_PATH);
答案 1 :(得分:0)
好, 我在这里错了...... 保存(写作)时你正在做
p.write(savePath + File.separator + fileName);
但是在调用“sendEmail”时,您只发送“fileName”, 不应该是“savePath + File.separator + fileName”吗?