Her er en lille demo til inspiration:
sign.asp
--------
<%
fnm = "foobar.jpg"
%>
<applet codebase="." code="SignatureApplet.class" width="750" height="250">
<param name="filename" value="<%=fnm%>">
</applet>
upload.asp
----------
<%
adTypeBinary = 1
adSaveCreateOverWrite=2
uploaddir = Server.MapPath("upload")
filename = Request.QueryString("filename")
data = Request.BinaryRead(Request.TotalBytes)
Set binstm = CreateObject("ADODB.Stream")
binstm.Type = adTypeBinary
binstm.Open
binstm.Write data
binstm.SaveToFile uploaddir & "\" & filename, adSaveCreateOverWrite
%>
SignatureApplet.java
--------------------
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class SignatureApplet extends JApplet implements ActionListener {
private final static String UPLOAD_URL = "
http://localhost/upload.asp"; private SignatureField sf;
public void init() {
setLayout(new BorderLayout());
sf = new SignatureField();
getContentPane().add(sf, BorderLayout.CENTER);
JButton btn = new JButton("Submit");
btn.addActionListener(this);
getContentPane().add(btn, BorderLayout.SOUTH);
}
@Override
public void actionPerformed(ActionEvent ev) {
try {
String filename = this.getParameter("filename");
URL url = new URL(UPLOAD_URL + "?filename=" + filename);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
ImageIO.write(sf.getImg(), "jpeg", os);
con.connect();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
JOptionPane.showMessageDialog(this, "Signature uploaded");
} else {
JOptionPane.showMessageDialog(this, "Signature not uploaded");
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "Signature not uploaded");
}
}
}
class SignatureField extends JPanel implements MouseMotionListener {
private final static int W = 750;
private final static int H = 250;
private BufferedImage img;
public SignatureField() {
img = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
Graphics g = img.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, W, H);
setPreferredSize(new Dimension(W, H));
this.addMouseMotionListener(this);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
public BufferedImage getImg() {
return img;
}
@Override
public void mouseDragged(MouseEvent ev) {
Graphics g = img.getGraphics();
g.setColor(Color.BLACK);
g.fillRect(ev.getPoint().x, ev.getPoint().y, 1, 1);
repaint();
}
@Override
public void mouseMoved(MouseEvent ev) {
}
}