---restore content starts---
1. Smartupload
This control is widely used in JSPs, and fileupload controls are primarily used in frames
2. If you want to use it, you need to copy the Smartupload jar package in the Tomcat Lib directory.
3. Upload a file through the form, because the file is hundreds of K, the method of the form must be post, it is impossible to use the Get method for address rewriting.
In addition, if you want to upload a file, you need to encapsulate the form Mutipart/form-data
Here are the smartupload_demo01.jsp
After the form is encapsulated, it is not possible to get the corresponding parameter property by using the request built-in object, because it is converted to binary form.
You can use Smart.getrequest (). GetParameter () to get
Upload image file is, in order to avoid the same name overlay problem, need to automatically generate a picture name when uploading.
Format is IP address + timestamp + random number
The above timestamp increases, you can write a Java class to implement.
Package lib.liys.timestamp;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Java.util.Random;
public class iptimestamp{
Private SimpleDateFormat SDF = null;
Private String IP = null;
Public Iptimestamp () {
}
Public iptimestamp (String IP) {
This.ip = IP;
}
Public String Getiptimerand () {
StringBuffer buf = new StringBuffer ();
if (this.ip!=null) {
String s[] = this.ip.split ("\ \"); Returns a string array by split
for (String x:s) {
Buf.append (Addzero (x,3));
}
}
Buf.append (This.gettimestamp ());
Random r = new Random (); 3-bit random integer
for (int i=0;i<3;i++) {
Buf.append (R.nextint (10));
}
return buf.tostring (); StringBuffer Convert to String
}
Public String Gettimestamp () {
THIS.SDF = new SimpleDateFormat ("Yyyymmddhhmmsssss"); Define a specific time format
Return This.sdf.format (New Date ()); Format date, return string
}
private string Addzero (string Str,int len) {
StringBuffer s = new StringBuffer ();
S.append (str);
while (S.length () < Len) {
S.insert (0, "0"); Buffer insert operation, insert "0" character from left
}
return s.tostring (); StringBuffer Convert to String
}
public static void Main (String args[]) {//write a main function to perform a
System.out.println (New Iptimestamp ("192.168.1.1"). Getiptimerand ());
}
}
If javac-d. *.java after compiling, want to perform see effect, need input java.lib.liys.timestamp.IPTimeStamp;
Finally write a JSP file
<% @page contenttype= "text/html" pageencoding= "GBK"%>
<% @page import = "org.lxh.smart.*"%>//jar package in the Lib directory under Tomcat, you need to look at the directory structure within the JAR package
<% @page import = "lib.liys.timestamp.*"%>//import timestamp class
<title>
Smartupload uploading a form
</title>
<body>
<% request.setcharacterencoding ("GBK"); %>
<%
Smartupload smart = new Smartupload ();
Smart.initialize (PageContext);//Initialize the upload operation
Smart.upload ();
String name = Smart.getrequest (). GetParameter ("uname");
Iptimestamp its = new Iptimestamp (REQUEST.GETREMOTEADDR ());
String ext = smart.getfiles (). GetFile (0). Getfileext (); Get Smart Extensions
String fileName = Its.getiptimerand () + "." + ext;
Smart.getfiles (). GetFile (0). SaveAs (This.getservletcontext (). Getrealpath ("/jspstudy/") + "upload" + Java.io.File.separator + fileName); Save File
%>
<%=smart.getfiles (). GetFile (0). GetFileName (). Matches ("^\\w+." ( Jpg|gif) ($ ")%>
">
</body>
If you are uploading multiple images, you need to loop through the
---restore content ends---
Java Web Learning Note-File Upload component for JSP Smartupload