uploadexample.jsp
<%@ page contenttype= "text/html;charset=gb2312"%>
<title><%=application.getserverinfo ()%></title>
<body>
Upload File Program Application example
<form action= "doupload.jsp" method= "post" enctype= "Multipart/form-data" >
<%--type enctype with Multipart/form-data, so that the data in the file as streaming data upload, no matter what file type, can be uploaded. --%>
Please select a file to upload <input type= "file" Name= "Upfile" size= ">
<input type= "Submit" value= "Submission" >
</form>
</body>
doupload.jsp
<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "java.io.*"%>
<%@ page import= "java.util.*"%>
<%@ pageimport= "javax.servlet.*"%>
<%@ pageimport= "javax.servlet.http.*"%>
<body bgcolor= "#ffffff" >
<%
Defines the maximum bytes of uploaded files
int max_size = 102400 * 102400;
Create a save variable for the root path
String RootPath;
Declaring a file read-in class
DataInputStream in = null;
FileOutputStream fileout = null;
Get the network address of the client
String remoteaddr = Request.getremoteaddr ();
Get the name of the server
String serverName = Request.getservername ();
Get the absolute address of the Internet program
String Realpath = Request.getrealpath (serverName);
Realpath = realpath.substring (0,realpath.lastindexof ("\ \"));
Create a saved directory for a file
RootPath = Realpath + "\\upload\\";
Get the type of data uploaded by the client
String ContentType = Request.getcontenttype ();
try{
if (Contenttype.indexof ("Multipart/form-data") >=0) {
Read the uploaded data
in = new DataInputStream (Request.getinputstream ());
int formdatalength = Request.getcontentlength ();
if (Formdatalength > Max_size) {
Out.println ("<P> uploaded files can not exceed the number of bytes +max_size +" </p> ");
Return
}
Save the uploaded file data
byte databytes[] = new Byte[formdatalength];
int byteread = 0;
int totalbytesread = 0;
The uploaded data is saved in a byte array
while (Totalbytesread < formdatalength) {
Byteread = In.read (databytes,totalbytesread,formdatalength);
Totalbytesread + = Byteread;
}
Create a string based on a byte array
String file = new string (databytes);
Out.println (file);
Get the file name of the uploaded data
String saveFile = file.substring (File.indexof ("filename=\") +10);
SaveFile = savefile.substring (0,savefile.indexof ("\ n"));
SaveFile = savefile.substring (savefile.lastindexof ("\ \") +1,savefile.indexof ("\" "));
int lastIndex = Contenttype.lastindexof ("=");
Get delimited string of data
String boundary = contenttype.substring (LastIndex +1,contenttype.length ());
Create a file name for the save path
String fileName = RootPath + saveFile;
Out.print (FileName);
int POS;
pos = File.indexof ("filename=\");
pos = File.indexof ("\ n", POS) + 1;
pos = File.indexof ("\ n", POS) + 1;
pos = File.indexof ("\ n", POS) + 1;
int boundarylocation = File.indexof (Boundary,pos)-4;
Out.println (boundarylocation);
Get the starting position of the file data
int startpos = ((file.substring (0,pos)). GetBytes ()). length;
Out.println (startpos);
Where to get the end of the file data
int endpos = ((file.substring (0,boundarylocation)). GetBytes ()). length;
Out.println (Endpos);
Check if the upload file exists
File Checkfile = new file (fileName);
if (checkfile.exists ()) {
Out.println ("<p>" + saveFile + "file already exists .</p>");
}
Check to see if the directory of uploaded files exists
File Filedir = new file (RootPath);
if (!filedir.exists ()) {
Filedir.mkdirs ();
}
Create a write-out class for a file
Fileout = new FileOutputStream (fileName);
Save the file's data
Fileout.write (Databytes,startpos, (Endpos-startpos));
Fileout.close ();
Out.println (saveFile + "file successfully uploaded .</p>");
}else{
String content = Request.getcontenttype ();
Out.println ("<p> uploaded data type is not multipart/form-data</p>");
}
}catch (Exception ex) {
throw new Servletexception (Ex.getmessage ());
}
%>
</body>
Run the method, put the two JSP files under the same path, run uploadexample.jsp.
Using JSP to implement the file upload function