Program is written in Eclipse, first put the Jspsmartupload.jar package under Web-inf/lib, and then import on the JSP page;
<!--the file upload class for smartupload components--
<%@ page import= "com.jspsmart.upload.*"%>
<%@ page import= "Com.jspsmart.upload.File"%>
Upload process:
<%
Smartupload mysmartupload =new smartupload ();
Long file_size_max=4000000;
String filename2= "", ext= "", testvar= "";
Initialization
Mysmartupload.initialize (PageContext);
Allow only upload of such files
try {
Mysmartupload.setallowedfileslist ("Jpg,gif");//The file format can be modified as needed
Uploading files
Mysmartupload.upload ();
} catch (Exception e) {
%>
<script type= "Text/javascript" >
Alert ("Only allow uploading of. jpg and. gif type picture files");
window.location= ' webdesign.jsp ';
</script>
<%
}
try{
Com.jspsmart.upload.File myFile = Mysmartupload.getfiles (). GetFile (0);
if (myfile.ismissing ()) {%>
<script type= "Text/javascript" >
Alert ("Please select a file to upload first");
window.location= ' webdesign.jsp ';
</script>
<%}
else{
String Myfilename=myfile.getfilename (); Gets the file name of the uploaded files
ext= Myfile.getfileext (); Get suffix Name
int file_size=myfile.getsize (); Get the size of the 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 ();
saveurl= "/upload/" + myfile.getfilename ();//Ensure that the presence of this upload directory exists in the root directory of Tomcat (that is, you need to create the appropriate folder yourself)
Myfile.saveas (saveurl);
Out.print (Saveurl);
}
}
}catch (Exception e) {
Out.print (E.tostring ());
}
%>
The red part is easy to cause the path error:
java.lang.IllegalArgumentException:This Path does not exist (1135)
The solution is to ensure that Tomcat has the presence of this upload directory in the root directory of the disk (that is, you need to create the appropriate folder yourself)
(In fact, write a function to determine whether the path exists, if it does not exist, the automatic creation of the directory should also be resolved, but not yet to try)
Path issue for uploading pictures using Jspsmartupload.jar