Sunday, February 17, 2013
I have already posted how you can upload a file to server with servlet. Today I will post how to download a file from server with servlet. You know how to send HTML data to client. But there may be situation where client may want to download a file hosted by server. So, here I will write the servlet code that will send the file to the client. The two important things that you will have to remember are :
--------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the project archive from Mediafire
DOWNLOAD the project archive from 4shared
NOTE : The archive does not contain the pdf. So remember to add it while using it.
- MIME type : You will have to set the content type. This is necessary as Container has no idea about type of file. If the type is set then the browser will automatically open the file in client's default application for that particular type.This is done using response.setContentType() method which takes in MIME type as parameter.
- I/O stream : You will have to use OutputStream and not PrintWriter. This is because we will be sending binary data and not HTML. This is done using response.getOutputStream() method which returns a reference to the object of java.io.OutputStream.
Screenshot of output
Output as seen in Firefox |
Java Source Code
--------------------------------------------------------------------------------------------------------------------------import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletContext; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/a/b/download") public class Downloader extends HttpServlet{ @Override public void doGet(HttpServletRequest req,HttpServletResponse res){ res.setContentType("application/pdf"); //setting MIME type ServletContext ctx = getServletContext(); try{ //getting stream to file InputStream is = ctx.getResourceAsStream("/ALG_3rd.pdf"); int read = 0; byte[] bytes = new byte[1024]; //setting buffer OutputStream os = res.getOutputStream(); //getting stream while ((read = is.read(bytes)) != -1) //reading file os.write(bytes, 0, read); //writing to stream os.flush(); //closing streams os.close(); }catch(Exception e){} } }
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------DOWNLOAD the project archive from Mediafire
DOWNLOAD the project archive from 4shared
NOTE : The archive does not contain the pdf. So remember to add it while using it.
Labels:J2EE | 0
comments
Saturday, February 9, 2013
Today I am going to post how you paint and stroke a shape in java using Graphics2D. But before going into the details of the program I would like to briefly describe about the painting and stroking operation.
• Painting is the process of filling the interior of the shape with a color, color gradient, or texture. The process of filling the shape interior is done in two steps :
• Painting is the process of filling the interior of the shape with a color, color gradient, or texture. The process of filling the shape interior is done in two steps :
- First, tell the Graphics2D how to fill shapes with a call to setPaint(). This method accepts any object that implements the java.awt.Paint interface. The Graphics2D stores the Paint away as part of its state. When it comes time to fill a shape, Graphics2D will use the Paint to determine what colors should be used to fill the shape. The 2D API comes with three kinds of "canned" paints: solid colors, a linear color gradient, and a texture fill. You can add your own Paint implementations if you wish.
- Now you can tell Graphics2Dto fill a shape by passing it to fill().
- First, tell the Graphics2D how you want the outline to be drawn by calling setStroke(). This method accepts any object that implements the java.awt.Stroke interface. The 2D API comes with a class, java.awt.BasicStroke, that implements common stroking options.
- Use setPaint() to tell the Graphics2D how the outline itself should be drawn. Outlines, like the interior of shapes, can be drawn using a color, a gradient, a texture, or anything else that implements the Paint interface.
- Draw the outline of the shape using Graphics2D's draw() method. The Graphics2D uses the Stroke from step 1 to determine what the outline looks like. The Paint from step 2 is used to actually render the outline.
Screenshot showing painting and stroking
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.BasicStroke;
import java.awt.GradientPaint;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.geom.Ellipse2D;
public class Painting_Stroking extends JPanel{
public static void main(String[] args) {
JFrame f=new JFrame("PaintingAndStroking v1.0");
Painting_Stroking obj= new Painting_Stroking();
f.getContentPane().add(obj);
f.setSize(320, 150);
f.setVisible(true);
f.setDefaultCloseOperation(f.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
double x = 15, y = 30, w = 70, h = 70; //defining shape attributes
Ellipse2D.Double e = new Ellipse2D.Double(x, y, w, h);
GradientPaint gp = new GradientPaint(75, 75, Color.white,
95, 95, Color.gray, true);
// Fill with a gradient
g2.setPaint(gp);
g2.fill(e); //filling first ellipse
// Stroke with a solid color
e.setFrame(x + 100, y, w, h);
g2.setPaint(Color.black);
g2.setStroke(new BasicStroke(8));
g2.draw(e); //drawing second ellipse with stroke and solid color
// Stroke with a gradient
e.setFrame(x + 200, y, w, h);
g2.setPaint(gp);
g2.draw(e); //drawing second ellipse with stroke and gradient
}
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
Labels:Graphics2D | 0
comments
Subscribe to:
Posts
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...