使用 Java 从 linux 上的图像获取路径
get path from an image on linux with Java
工作环境:
windows7、
软呢帽 20,
Java 7_79,
行家,
日食
编辑
我有一个使用 maven 的网络 java 项目,我正在尝试获取我在 src/main/resouces 包中的图像以将图像放在电子邮件中,稍后将此图像设置为文件夹 WEB-INF/classes/html 中的 war。当 运行 windows 7 的项目一切正常,但问题是当我 运行 我在 fedora 20 (linux) 上的 web 项目时,我得到下一个错误:
> 21:26:09,662 ERROR [stderr] (Thread-92) java.lang.NullPointerException
> 21:26:09,662 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
> 21:26:09,663 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
> 21:26:09,663 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
> 21:26:09,663 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
> 21:26:09,664 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
> 21:26:09,664 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
> 21:26:09,664 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
> 21:26:09,665 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
> 21:26:09,665 ERROR [stderr] (Thread-92) at
> javax.mail.Transport.send(Transport.java:123) 21:26:09,666 ERROR
> [stderr] (Thread-92) at
> com.acertum.serc.util.SolicitudEmail.enviarCorreo(SolicitudEmail.java:148)
> 21:26:09,666 ERROR [stderr] (Thread-92) at
> com.acertum.serc.controller.AgregarExcepcionView.run(AgregarExcepcionView.java:383)
这是获取图片路径的方法
public MimeBodyPart obtenerImagen(String urlImagen) throws IOException, MessagingException{
logger.info("entro");
URL url = getClass().getClassLoader().getResource(urlImagen);
logger.info("url" + url);
MimeBodyPart attachmentPart = new MimeBodyPart();
logger.info("url.getPath()" + url.getPath().toString());
attachmentPart.attachFile(url.getPath());
attachmentPart.setDisposition(MimeBodyPart.INLINE);
return attachmentPart;
}
我发送电子邮件的主要方法是下一个:
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(ScoreConstants.getFromSolicitudEmail()));
message.setRecipients(Message.RecipientType.TO, recipientes);
message.setSubject(ScoreConstants.getSubjectSolicitudEmail());
String fechaSistema="<html><body><br/><br/><br/><div align='right'><h3>México,D.F a "+date+"</h3></div><br/>";
String nombreAcreditado="<br/><br/><div style='font-family: Calibri;'><strong>Acreditado: </strong> "+ acreditado +"</div><br/>";
String tipoproducto="<div style='font-family: Calibri;'><strong>Tipo de Producto: </strong>"+ subproducto +"</div><br/>";
String numeroMonto="<div style='font-family: Calibri;'><strong>Monto: </strong>"+ monto +"</div><br/>";
String descripcionExcepcion="<div style='font-family: Calibri;'><strong>Descripcion de la excepción: </strong>"+ descripcion +"</div><br/>";
String linkAceptada = "<br/><br/><br/><br/><br/><br/><br/><br/><div style='margin-left: 350px;font-family: Calibri;'><a href='http://"+ ScoreConstants.getIpServidorWeb()+":"+ScoreConstants.getPuertoServidorWeb()+"/ScoreExcepciones/AceptadoRechazado.xhtml?nombre="
+ acreditado
+ "&celular="
+ celular
+ "&fase="
+ fase
+ "&excepcionId="
+ excepcionId
+ "&subproductoId="
+ subproductoId
+ "&version="
+ version
+ "&empleadoId="
+ empleadoId
+ "&ipAddres="
+ ipAddress
+ "&accion="+autorizar+"'>Autorizar</a> "
+ " <a href='http://"+ScoreConstants.getIpServidorWeb()+":"+ScoreConstants.getPuertoServidorWeb()+"/ScoreExcepciones/AceptadoRechazado.xhtml?nombre="
+ acreditado
+ "&celular="
+ celular
+ "&fase="
+ fase
+ "&excepcionId="
+ excepcionId
+ "&subproductoId="
+ subproductoId
+ "&version="
+ version
+ "&empleadoId="
+ empleadoId
+ "&ipAddres="
+ ipAddress
+ "&accion="
+ rechazar+ "'>Rechazar</a></div>";
String cadena = String.format("%s%s%s%s%s%s%s", fechaSistema,
obtenerRutaContenido(File.separator + "html" + File.separator + "Correo.html"),
nombreAcreditado, tipoproducto, numeroMonto,
descripcionExcepcion, linkAceptada);
MimeBodyPart cadenaContenido = new MimeBodyPart();
cadenaContenido.setContent(cadena, "text/html");
String rutaPiePagina=obtenerRutaContenido((File.separator +"html" + File.separator + "PiePagina.html"));
MimeBodyPart piePagina = new MimeBodyPart();
piePagina.setContent(rutaPiePagina, "text/html");
MimeBodyPart imagenCreada=obtenerImagen(("html" + File.separator + "Logo-AcertumBank-S.jpg"));
message.setContent(ordenContenidos(imagenCreada,cadenaContenido,piePagina));
Transport.send(message);
} catch (AddressException e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
} catch (MessagingException e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
}
obtenerRutaContenido(File.separator + "html" + File.separator + "Correo.html"),
...
obtenerRutaContenido((File.separator +"html" + File.separator + "PiePagina.html"));
...
obtenerImagen(("html" + File.separator + "Logo-AcertumBank-S.jpg"));
如果 obtenerRutaContenido()
和 obtenerImagen()
相似,您也需要以 File.separator
开始路径。
主要是你没有有一个文件(在文件系统上)但是有一个资源(在jar/war/class路径/...)。
public MimeBodyPart obtenerImagen(String urlImagen) throws IOException, MessagingException {
logger.info("entro");
URL url = getClass().getClassLoader().getResource(urlImagen);
MimeBodyPart attachmentPart = new MimeBodyPart(url.openStream());
attachmentPart.setFileName(url.getFile()); // Just "xyz.jpg" maybe
attachmentPart.setDisposition(MimeBodyPart.INLINE);
//attachmentPart.setContentType("image/jpeg");
return attachmentPart;
}
这会将资源的 InputStream 带到 MimeBodyPart 构造函数以填充其中的内容。
工作环境: windows7、 软呢帽 20, Java 7_79, 行家, 日食
编辑
我有一个使用 maven 的网络 java 项目,我正在尝试获取我在 src/main/resouces 包中的图像以将图像放在电子邮件中,稍后将此图像设置为文件夹 WEB-INF/classes/html 中的 war。当 运行 windows 7 的项目一切正常,但问题是当我 运行 我在 fedora 20 (linux) 上的 web 项目时,我得到下一个错误:
> 21:26:09,662 ERROR [stderr] (Thread-92) java.lang.NullPointerException
> 21:26:09,662 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:226)
> 21:26:09,663 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeUtility.getEncoding(MimeUtility.java:299)
> 21:26:09,663 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1375)
> 21:26:09,663 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1021)
> 21:26:09,664 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeMultipart.updateHeaders(MimeMultipart.java:419)
> 21:26:09,664 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeBodyPart.updateHeaders(MimeBodyPart.java:1354)
> 21:26:09,664 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeMessage.updateHeaders(MimeMessage.java:2107)
> 21:26:09,665 ERROR [stderr] (Thread-92) at
> javax.mail.internet.MimeMessage.saveChanges(MimeMessage.java:2075)
> 21:26:09,665 ERROR [stderr] (Thread-92) at
> javax.mail.Transport.send(Transport.java:123) 21:26:09,666 ERROR
> [stderr] (Thread-92) at
> com.acertum.serc.util.SolicitudEmail.enviarCorreo(SolicitudEmail.java:148)
> 21:26:09,666 ERROR [stderr] (Thread-92) at
> com.acertum.serc.controller.AgregarExcepcionView.run(AgregarExcepcionView.java:383)
这是获取图片路径的方法
public MimeBodyPart obtenerImagen(String urlImagen) throws IOException, MessagingException{
logger.info("entro");
URL url = getClass().getClassLoader().getResource(urlImagen);
logger.info("url" + url);
MimeBodyPart attachmentPart = new MimeBodyPart();
logger.info("url.getPath()" + url.getPath().toString());
attachmentPart.attachFile(url.getPath());
attachmentPart.setDisposition(MimeBodyPart.INLINE);
return attachmentPart;
}
我发送电子邮件的主要方法是下一个:
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(ScoreConstants.getFromSolicitudEmail()));
message.setRecipients(Message.RecipientType.TO, recipientes);
message.setSubject(ScoreConstants.getSubjectSolicitudEmail());
String fechaSistema="<html><body><br/><br/><br/><div align='right'><h3>México,D.F a "+date+"</h3></div><br/>";
String nombreAcreditado="<br/><br/><div style='font-family: Calibri;'><strong>Acreditado: </strong> "+ acreditado +"</div><br/>";
String tipoproducto="<div style='font-family: Calibri;'><strong>Tipo de Producto: </strong>"+ subproducto +"</div><br/>";
String numeroMonto="<div style='font-family: Calibri;'><strong>Monto: </strong>"+ monto +"</div><br/>";
String descripcionExcepcion="<div style='font-family: Calibri;'><strong>Descripcion de la excepción: </strong>"+ descripcion +"</div><br/>";
String linkAceptada = "<br/><br/><br/><br/><br/><br/><br/><br/><div style='margin-left: 350px;font-family: Calibri;'><a href='http://"+ ScoreConstants.getIpServidorWeb()+":"+ScoreConstants.getPuertoServidorWeb()+"/ScoreExcepciones/AceptadoRechazado.xhtml?nombre="
+ acreditado
+ "&celular="
+ celular
+ "&fase="
+ fase
+ "&excepcionId="
+ excepcionId
+ "&subproductoId="
+ subproductoId
+ "&version="
+ version
+ "&empleadoId="
+ empleadoId
+ "&ipAddres="
+ ipAddress
+ "&accion="+autorizar+"'>Autorizar</a> "
+ " <a href='http://"+ScoreConstants.getIpServidorWeb()+":"+ScoreConstants.getPuertoServidorWeb()+"/ScoreExcepciones/AceptadoRechazado.xhtml?nombre="
+ acreditado
+ "&celular="
+ celular
+ "&fase="
+ fase
+ "&excepcionId="
+ excepcionId
+ "&subproductoId="
+ subproductoId
+ "&version="
+ version
+ "&empleadoId="
+ empleadoId
+ "&ipAddres="
+ ipAddress
+ "&accion="
+ rechazar+ "'>Rechazar</a></div>";
String cadena = String.format("%s%s%s%s%s%s%s", fechaSistema,
obtenerRutaContenido(File.separator + "html" + File.separator + "Correo.html"),
nombreAcreditado, tipoproducto, numeroMonto,
descripcionExcepcion, linkAceptada);
MimeBodyPart cadenaContenido = new MimeBodyPart();
cadenaContenido.setContent(cadena, "text/html");
String rutaPiePagina=obtenerRutaContenido((File.separator +"html" + File.separator + "PiePagina.html"));
MimeBodyPart piePagina = new MimeBodyPart();
piePagina.setContent(rutaPiePagina, "text/html");
MimeBodyPart imagenCreada=obtenerImagen(("html" + File.separator + "Logo-AcertumBank-S.jpg"));
message.setContent(ordenContenidos(imagenCreada,cadenaContenido,piePagina));
Transport.send(message);
} catch (AddressException e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
} catch (MessagingException e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
}
obtenerRutaContenido(File.separator + "html" + File.separator + "Correo.html"),
...
obtenerRutaContenido((File.separator +"html" + File.separator + "PiePagina.html"));
...
obtenerImagen(("html" + File.separator + "Logo-AcertumBank-S.jpg"));
如果 obtenerRutaContenido()
和 obtenerImagen()
相似,您也需要以 File.separator
开始路径。
主要是你没有有一个文件(在文件系统上)但是有一个资源(在jar/war/class路径/...)。
public MimeBodyPart obtenerImagen(String urlImagen) throws IOException, MessagingException {
logger.info("entro");
URL url = getClass().getClassLoader().getResource(urlImagen);
MimeBodyPart attachmentPart = new MimeBodyPart(url.openStream());
attachmentPart.setFileName(url.getFile()); // Just "xyz.jpg" maybe
attachmentPart.setDisposition(MimeBodyPart.INLINE);
//attachmentPart.setContentType("image/jpeg");
return attachmentPart;
}
这会将资源的 InputStream 带到 MimeBodyPart 构造函数以填充其中的内容。