Teach you custom single file upload code for pure JSP

Source: Internet
Author: User
Tags array file upload string client
Js

This article is a custom single file upload code for a pure JSP:

<%@ page contenttype= "text/html; CHARSET=GBK "%>
<%@ page import= "java.io.*"%>
<%@ page import= "java.util.*"%>
<%@ page import= "javax.servlet.*"%>
<%@ page import= "javax.servlet.http.*"%>
<title>
Upfile
</title>
<body bgcolor= "#ffffff" >
<center>
<%
Define maximum bytes for uploaded files
int max_size = 102400 * 102400;
To create a save variable for the root path
String RootPath;

Declaring a file read into a class
DataInputStream in = null;
FileOutputStream fileout = null;
Get the client's network address
String remoteaddr = Request.getremoteaddr ();
Out.print (REMOTEADDR);
Get the name of the server
String serverName = Request.getservername ();
Out.print (ServerName);
Gets the address of the JSP file relative to the root address
Out.print (Request.getservletpath ());
Get an absolute address for an Internet program
String Realpath = Request.getrealpath (serverName);
Out.println (Realpath);
Realpath = realpath.substring
(0,realpath.lastindexof ("\"));
Out.print (Realpath);
Create the Save directory for the file "\upload"
RootPath = Realpath + "\\upload\\";
Out.println (RootPath);
Get the data type that the client uploads
String ContentType = Request.getcontenttype ();
Out.println
("<p> client uploaded data type =
"+ ContentType + </p>");
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> the number of bytes uploaded can not exceed"
+ max_size + "</p>");
Return
}
Save data for uploaded files
byte databytes[] = new Byte[formdatalength];
int byteread = 0;
int totalbytesread = 0;
The uploaded data is saved in the byte array
while (Totalbytesread < formdatalength)
{
Byteread = In.read (Databytes,
Totalbytesread,formdatalength);
Totalbytesread + = Byteread;
}
Create a string from a byte array
String file = new string (databytes);
Out.println (file);
Gets 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 ("=");
Gets the 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);
Gets the location where the file data starts
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 uploaded file exists
File Checkfile = new file (fileName);
if (checkfile.exists ()) {
Out.println ("<p>" + SaveFile +
"File already exists .</p>");
}
Check if the directory of uploaded files exists
File Filedir = new file (RootPath);
if (!filedir.exists ())
{
Filedir.mkdirs ();
}
Create a write class for a file
Fileout = new FileOutputStream (fileName);
Save data for a file
Fileout.write (Databytes,startpos,
(Endpos-startpos));
Fileout.close ();
Out.println ("<P>" + SaveFile +
"File successfully uploaded .</p>");
}else{
String content = Request.getcontenttype ();
Out.println
("The data type of the <p> upload is not multipart/form-data</p>");
}
}catch (Exception ex)
{
throw new Servletexception (Ex.getmessage ());
}
%>
</center>
</body>


Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.