Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Image resize : Java

0

Posted by Jagadeesh VP | Posted in ,

Here is the code to resize an Image


private BufferedImage resizeImage(File imageFile) {
int width = 250;
int height = 250;

BufferedImage image = null;
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {

}

BufferedImage resizedImage = new BufferedImage(width, height, image.getType());
Graphics2D g = resizedImage.createGraphics();
g.drawImage(image, 0, 0, width, height, null);
g.dispose();
return resizedImage;
}


To write an image as file

ImageIO.write(image, extension, file);

image --> RenderedImage; My case this is BufferedImage
extension --> file type
file --> your output file object

Authentication - Java Mail

0

Posted by Jagadeesh VP | Posted in ,


import java.net.URLEncoder;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class MyMail {

private String d_email = "";
private String d_password = "";

public void sendMail(String tomailid, String subject, String message)
throws Exception {

Properties mailProps = new Properties();

mailProps.load( this.getClass().getResourceAsStream("data/MailData.properties"));

d_email = mailProps.getProperty("smtp.user");
d_password = mailProps. getProperty("smtp.password");

Authenticator auth = new SMTPAuthenticator();

Session session = Session.getInstance(mailProps, auth);

MimeMessage msg = new MimeMessage(session);
msg.setHeader("Content-Type", "text/html;");

InternetAddress from = new InternetAddress(mailProps.get("mail.user").toString(),
"[EMAIL_HEADING]");
msg.setFrom(from);
InternetAddress to = new InternetAddress(tomailid);

msg.addRecipient(Message.RecipientType.TO, to);
msg.addRecipient(Message.RecipientType.BCC, new InternetAddress("emailid@test.in"));
msg.setSubject(subject);
msg.setContent(message, "text/html");

Transport.send(msg);
}

private class SMTPAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
}

}

InputStream to String

0

Posted by Jagadeesh VP | Posted in ,


import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class StreamToString {

public static void main(String[] args) throws Exception {
StreamToString sts = new StreamToString();

InputStream is = sts.getClass().getResourceAsStream("/data.txt");

/*
* Call the method to convert the
* stream to string
*/
System.out.println(sts.convertStreamToString(is));
}

public String convertStreamToString(InputStream is) throws IOException {
/*
* To convert the InputStream to String
* we use the BufferedReader.readLine()
* method. Each line will appended to a
* StringBuilder and returned as String.
*/
if (is != null) {
StringBuilder sb = new StringBuilder();
String line = null;

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null)
sb.append(line).append("\n");

} catch (Exception e) {
//Do something..
} finally {
is.close();
}
return sb.toString();
} else {
return "";
}
}
}

Memory Monitor JSP

0

Posted by Jagadeesh VP | Posted in , ,

Place this JSP in your container to know the memory status

<%@ page import="java.lang.management.*" %>
<%@ page import="java.util.*" %>

<title>JVM Memory Monitor</title>
<%
Iterator<MemoryPoolMXBean> iterator = ManagementFactory.getMemoryPoolMXBeans().iterator();
while (iterator.hasNext()) {
MemoryPoolMXBean item = (MemoryPoolMXBean) iterator.next();
%>

<table border="0" width="100%">
<tbody><tr>
<td colspan="2" align="center">
<h3>Memory MXBean</h3>
</td>
</tr>
<tr>
<td width="200">Heap Memory Usage</td>
<td>
<%= ManagementFactory.getMemoryMXBean().getHeapMemoryUsage()%>
</td>
</tr>
<tr>
<td>Non-Heap Memory Usage</td>
<td>
<%= ManagementFactory.getMemoryMXBean().getNonHeapMemoryUsage() %>
</td>
</tr>
<% } %>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2" align="center">
<h3>Memory Pool MXBeans</h3>
</td>
</tr>
<%
Iterator<MemoryPoolMXBean> iter1 = ManagementFactory.getMemoryPoolMXBeans().iterator();
while (iter1.hasNext()) {
MemoryPoolMXBean item = (MemoryPoolMXBean) iter1.next();
%>
<tr>
<td colspan="2">
<table style="border: 1px solid rgb(152, 170, 177);" border="0" width="100%">
<tbody><tr>
<td colspan="2" align="center">
<b><%= item.getName() %></b>
</td>
</tr>
<tr>
<td width="200">Type</td>
<td>
<%= item.getType() %>
</td>
</tr>
<tr>
<td>Usage</td>
<td><%= item.getUsage() %></td>
</tr>
<tr>
<td>Peak Usage</td>
<td><%= item.getPeakUsage() %></td>
</tr>
<tr>
<td>Collection Usage</td>
<td><%= item.getCollectionUsage() %></td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<% } %>
</tbody></table>