JSP image File Upload code
<%@ page contenttype= "Text/html;charset=utf-8" language= "java" import= "Java.io.*,java.awt.image,java.awt.image. *,com.sun.image.codec.jpeg.*,
Java.sql.*,com.jspsmart.upload.*,java.util.*,cn.oof.database.*,cn.oof.house.* "%>
<%
Smartupload mysmartupload =new smartupload ();
Long file_size_max=4000000;
String filename2= "", ext= "", testvar= "";
String url= "uploadfile/"; Should be guaranteed to have this directory presence in the root directory
Class
Mysmartupload.initialize (PageContext);
Only allow this type of file to be uploaded
try {
Mysmartupload.setallowedfileslist ("Jpg,gif,bmp");
Uploading files
Mysmartupload.upload ();
catch (Exception e) {
%>
<script language=javascript>
Alert ("Only allow uploading of. jpg,. bmp and. gif type picture files");
window.location= ' upload.htm ';
</script>
<%
}
try{
Com.jspsmart.upload.File myFile = Mysmartupload.getfiles (). GetFile (0);
if (myfile.ismissing ()) {%>
<script language=javascript>
Alert ("Please select the file to upload first");
window.location= ' upload.htm ';
</script>
<%}
else{
String Myfilename=myfile.getfilename (); Gets the filename of the uploaded file
ext= Myfile.getfileext (); Get suffix Name
int file_size=myfile.getsize (); To get the size of a file
String saveurl= "";
if (File_size<file_size_max) {
Change the file name to get the millisecond value of the current upload time
Calendar calendar = Calendar.getinstance ();
String filename = string.valueof (Calendar.gettimeinmillis ());
Saveurl=request.getrealpath ("/") +url;
Saveurl+=filename+ "." +ext; Save path
String newpic=url+filename+ "." +ext;
Myfile.saveas (saveurl,mysmartupload.save_physical);
Out.print (filename);
-----------------------upload completed, start generating thumbnails-------------------------
Java.io.File File = new Java.io.File (Saveurl); Read the file you just uploaded.
String Newurl=request.getrealpath ("/") +url+ "Min_" +filename+ "." +ext; New thumbnail Save Address
Image src = javax.imageio.ImageIO.read (file); Constructing an Image Object
float tagsize=200;
int old_w=src.getwidth (NULL); Get Source image width
int old_h=src.getheight (NULL);
int new_w=0;
int new_h=0; Get source Diagram length
int tempsize;
float tempdouble;
if (Old_w>old_h) {
Tempdouble=old_w/tagsize;
}else{
Tempdouble=old_h/tagsize;
}
New_w=math.round (old_w/tempdouble);
New_h=math.round (old_h/tempdouble)//Calculation of the new graph long width
BufferedImage tag = new BufferedImage (NEW_W,NEW_H,BUFFEREDIMAGE.TYPE_INT_RGB);
Tag.getgraphics (). DrawImage (Src,0,0,new_w,new_h,null); Draw a smaller figure
FileOutputStream newimage=new FileOutputStream (Newurl); Output to file stream
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (newimage);
Encoder.encode (tag); Near-JPEG coding
Newimage.close ();
Out.print ("<script language= ' JavaScript ' >");
Out.print ("alert (' File upload succeeded ');");
Out.print ("Parent.document.form1.img.value =" "+newpic+");
Out.print ("window.location= ' upload.htm ';");
Out.print ("</SCRIPT>");
}
else{
Out.print ("<script language= ' JavaScript ' >");
Out.print ("alert (' upload file size cannot exceed" + (file_size_max/1000) + "K ');");
Out.print ("window.location= ' upload.htm ';");
Out.print ("</SCRIPT>");
}
}
}catch (Exception e) {
E.tostring ();
}
%>