js| upload | upload picture | Thumbnail This example uses the Jspsmart component for uploading, which can be downloaded for free www.jspsmart.com
After the decompression, the jar package is copied to the \web-inf\lib directory to restart the server, Jspsmart can be used normally
1, uploadimage.jsp
<%@ page contenttype= "text/html;charset=gb2312" 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/images/"; 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");
Uploading files
Mysmartupload.upload ();
catch (Exception e) {
%>
<script language=javascript>
Alert ("Only allow uploading of. jpg and. gif type picture files");
window.location= ' upfile.jsp ';
</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= ' upfile.jsp ';
</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
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+filename+ "_min." +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 ();
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.