You are on page 1of 4

c 

  

<form name="uploadForm" action="upload.jsp" enctype="multipart/form-data"
method="post">
<input type="file" name="file"/>
<input TYPE=Button name='Upload' Value='Upload'
onClick="uploadForm.Upload.value='Uploading...';document.uploadForm.action='u
pload.jsp';document.uploadForm.submit()">
</form>
}



<%

response.setContentType("text/html");
response.setHeader("Cache-control","no-cache");

String err = "";

String lastFileName = "";

String contentType = request.getContentType();


String boundary = "";
   BOUNDARY_WORD_SIZE = "boundary=".length();
(contentType ==  || !contentType.startsWith("multipart/form-data"))
{
err = "Ilegal ENCTYPE : must be multipart/form-data\n";
err += "ENCTYPE set = " + contentType;
}{
boundary = contentType.substring(contentType.indexOf("boundary=") +
BOUNDARY_WORD_SIZE);
boundary = "--" + boundary;
 {
javax.servlet.ServletInputStream sis = request.getInputStream();
[] b =  [1024];
 x=0;
 state=0;
String name= ,fileName= ,contentType2= ;
java.io.FileOutputStream buffer = ;
 ((x=sis.readLine(b,0,1024))>-1) {
String s =  String(b,0,x);
(s.startsWith(boundary)) {
state = 0;
//out.println("name="+name+"<br>");
//out.println(fileName+"<br>");

name = ;
contentType2 = ;
fileName = ;

} (s.startsWith("Content-Disposition") && state==0) {


state = 1;
(s.indexOf("filename=") == -1)
name = s.substring(s.indexOf("name=") +
"name=".length(),s.length()-2);
 {
name = s.substring(s.indexOf("name=") +
"name=".length(),s.lastIndexOf(";"));
fileName = s.substring(s.indexOf("filename=") +
"filename=".length(),s.length()-2);
(fileName.equals("\"\"")) {
fileName = ;
} {
String userAgent = request.getHeader("User-Agent");
String userSeparator="/"; // default
 (userAgent.indexOf("Windows")!=-1)
userSeparator="\\";
 (userAgent.indexOf("Linux")!=-1)
userSeparator="/";
fileName =
fileName.substring(fileName.lastIndexOf(userSeparator)+1,fileName.length()-
1);
(fileName.startsWith( "\""))
fileName = fileName.substring( 1);
}
}
name = name.substring(1,name.length()-1);
 (name.equals("file")) {
 (buffer!= )
buffer.close();
lastFileName = fileName;
buffer =  java.io.FileOutputStream("/Program Files/Apache
Group/Tomcat 4.1/webapps/jspcart/images/"+fileName);
}
} (s.startsWith("Content-Type") && state==1) {
state = 2;
contentType2 = s.substring(s.indexOf(":")+2,s.length()-2);
} (s.equals("\r\n") && state != 3) {
state = 3;
} {
 (name.equals("file"))
buffer.write(b,0,x);
}
}
sis.close();
buffer.close();
} (java.io.IOException e) {
err = e.toString();
}
}
 ok = err.equals("");
(!ok) {
out.println(err);
}{
%>
<SCRIPT language="javascript">
history.back(1)
alert('Uploaded <%=lastFileName%>');
window.location.reload( )
</SCRIPT>
<%
}
out.println("done");
%>

ÊÊÊ 
Ê ÊÊÊÊ

Ê  Ê  


  
 


 
      › Ê

   insertImage(Image image)  SQLException {


Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn =
DriverManager.getConnection("jdbc:mysql://"+"db_address");
 id = -1;
String sql = "insert into test (img) values (?)\n";
PreparedStatement ps =  .getStatement(sql); // this method is
trivial
 [] bytes =  .getBytes(image); // * see below
ps.setBytes(1, bytes);
ps.executeUpdate();
id = ps.getGeneratedKeys().getInt(0); //Actually I couldn't make this
line work yet.
 id;
}

  Image  (  id)  SQLException {


Image img = ;
String sql = "select img from test where id = ?\n";
PreparedStatement ps = getStatement(sql);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
 (rs.next()) {
 [] bytes = rs.getBytes(1);
img =  .getImage(bytes); // * see below
}
 img;
}
   [] getBytes(Image image) {
ByteArrayOutputStream baos =  ByteArrayOutputStream();
 {
ImageIO.„rite( .getBufferedImage(image), "JPEG", baos);
}   (IOException ex) {
  RuntimeException(ex);
}
 baos.toByteArray();
}
  Image getImage( [] bytes) {
Image image = ;
ByteArrayInputStream bais =  ByteArrayInputStream(bytes);
 {
image = ImageIO.read(bais);
}   (IOException ex) {
  RuntimeException(ex);
}
 image;
}

  BufferedImage getBufferedImage(Image image) {


 width = image.getWidth( );
 height = image.getHeight( );
BufferedImage bi =  BufferedImage(width, height,
BufferedImage.ë Në);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(image, 0, 0, );
 bi;