Before have done an upload image function, found the data are all used in the framework to do, at that time in order to complete the task as soon as possible, take over to repair the changes directly put on the line. For my rookie, although the task is completed, but at present more important is the personal technical improvement. Then I started to think again. The manager said at ordinary times to look at the basis of more, well, work also has a year, years later I also wrote a diary of a profound reflection, and then re-positioning themselves. As a matter of course, the first layer is to buy furniture for people, for all kinds of technology is just to study what it is to do, how to use. It can only be used, and it will only be used. Ability is not enough to thoroughly understand its bottom and core. A lot of technology can only be used. Although this is necessary to do the project, and I can also qualified. But from a personal point of view, such things are repeated more, it is really moving bricks. I would like to become a furniture man step, become a tree-planting people ...
Think more than the practical to engage in something, well, open to engage. At the end of the mission, I began to try to do this with the basic servlet. The principle of uploading I personally think that is to write the local file as an input stream to a directory on the server. Well, following this logic, I started looking for the method provided by Reques, HttpServletRequest did not provide the relevant method, but ServletRequest provided a getInputStream () method, This method returns the binary data of the entity that retrieved the ServletInputStream request. Well, it's a little bit reliable. View the ServletInputStream API, which explains that this abstract class provides an input stream that reads binary data from the client, and contains an efficient ReadLine method that is implemented by container. OK, the input stream is found, and the rest is to write the read data to the corresponding file in a directory.
To have a JSP, submit the data
<body>
<form action= "Upload" method= "post" enctype= "Multipart/form-data" >
<input type= "file" name= "Upload" >
<input type= "Submit" value= "Submit" >
</form>
</body>
The rest is to get the data in the background and write it.
public class Uploadaction extends HttpServlet {
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Doget (req, resp);
}
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
InputStream In=req.getinputstream ();
OutputStream Out=null;
String path= "C:\\users\\administrator\\desktop\\promo\\sub\\test.jpg";
Out=new FileOutputStream (path);
int c=0;
while ((C=in.read ())!=-1) {
Out.write (c);
Out.flush ();
}
In.close ();
Out.close ();
}
}
I use the picture test, after running, the path has a test.jpg, but open the prompt file has been damaged. I have a big 艹, again this problem! Use Firebug to see what data is being submitted is no problem. Where is this Nima problem? Follow the process, get the data, write the data, the data can be written, no problem, that is to get the read data there is a problem. Change the test.jpg to Test.txt run after open a look, the file unexpectedly has three lines Ming character, the first sentence corresponds to Content-type:multipart/form-data; boundary=----WEBKITFORMBOUNDARYOGNVQ83GCRDVABMQ, is the value of boundary, the second sentence is content-disposition:form-data; Name= "Upload"; Filename= "1.jpg" contains the parameter name of the file in the JSP, and the value of filename is the name of the upload. The third line is Content-type:image/jpeg, which is the header field content. Line four is blank. That is, from the fourth line is the contents of the file, that is, the servletinputstream so-called Grass mud Horse request entity binary data! My English is very poor Ah! Retrieves the body of the request as binary data using a servletinputstream, what the fuck is the body of the quest! Well, first of all, know where the problem is, then bypass the pit, since the content is not exactly uploaded file content, it will need to be not the content of the file to be discarded. My idea is to put getinputstream () or take the contents of a temporary TXT file, and then read the contents of the real upload file from this file. Well, it works. The next step is to introduce a new member, Randomaccessfile. I am not familiar with it, but also used to understand, well, at this moment, it is my hero. A random file access stream that can find or write data anywhere in the file. This feature, is invincible anti-tamper, invincible fixed damage! OK, the code continues to change
public class Uploadaction extends HttpServlet {
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Doget (req, resp);
}
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
InputStream In=req.getinputstream ();
OutputStream Out=null;
OutputStream Fout=null;
String path= "C:\\users\\administrator\\desktop\\promo\\sub\\test.txt";
Randomaccessfile raf=new randomaccessfile (path, "R");
String filepath= "C:\\users\\administrator\\desktop\\promo\\sub\\test.jpg";
Out=new FileOutputStream (path);
Fout=new FileOutputStream (FilePath);
int c=0;
while ((C=in.read ())!=-1) {
Out.write (c);
Out.flush ();
}
This.log (Raf.readline ());
This.log (Raf.readline ());
This.log (Raf.readline ());
This.log (Raf.readline ());
int d=0;
while ((D=raf.read ())!=-1) {
Fout.write (d);
Fout.flush ();
}
Fout.close ();
In.close ();
Out.close ();
}
}
After running the picture is normal to come out. And then continue to improve, since the name of the uploaded image can be read, then let's deal with it, so that the name corresponds to
public class Uploadaction extends HttpServlet {
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Doget (req, resp);
}
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
InputStream In=req.getinputstream ();
OutputStream Out=null;
OutputStream Fout=null;
String path= "C:\\users\\administrator\\desktop\\promo\\sub\\temp.txt";
String filepath= "c:\\users\\administrator\\desktop\\promo\\sub\\";
Out=new FileOutputStream (path);
int c=0;
while ((C=in.read ())!=-1) {
Out.write (c);
Out.flush ();
}
String fileName = null;
Randomaccessfile raf=new randomaccessfile (path, "R");
This.log (Raf.readline ());
String []tempname=raf.readline (). Split (";");
for (String s:tempname) {
if (s.contains ("filename")) {
String[] Ss=s.split ("=");
FILENAME=SS[SS.LENGTH-1];
}
}
This.log (Raf.readline ());
This.log (Raf.readline ());
Fout=new FileOutputStream (filepath+filename.replace ("\" "," "));
int d=0;
while ((D=raf.read ())!=-1) {
Fout.write (d);
Fout.flush ();
}
Fout.close ();
In.close ();
Out.close ();
Raf.close ();
File File=new file (filepath+ "Temp.txt". Replace ("\" "," "));
if (file.exists ()) {
if (File.delete ()) {
This.log ("finished");
}
}
}
}
Here, the function of uploading a file has been implemented, but there is a point, is that the Temp.txt file, and finally to delete it, so it is more complete, or the total sense of something more
public class Uploadaction extends HttpServlet {
@Override
protected void Doget (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
Doget (req, resp);
}
@Override
protected void DoPost (HttpServletRequest req, HttpServletResponse resp)
Throws Servletexception, IOException {
InputStream In=req.getinputstream ();
OutputStream Out=null;
OutputStream Fout=null;
String path= "C:\\users\\administrator\\desktop\\promo\\sub\\temp.txt";
String filepath= "c:\\users\\administrator\\desktop\\promo\\sub\\";
Out=new FileOutputStream (path);
int c=0;
while ((C=in.read ())!=-1) {
Out.write (c);
Out.flush ();
}
String fileName = null;
Randomaccessfile raf=new randomaccessfile (path, "R");
This.log (Raf.readline ());
String []tempname=raf.readline (). Split (";");
for (String s:tempname) {
if (s.contains ("filename")) {
String[] Ss=s.split ("=");
FILENAME=SS[SS.LENGTH-1];
}
}
This.log (Raf.readline ());
This.log (Raf.readline ());
Fout=new FileOutputStream (filepath+filename.replace ("\" "," "));
int d=0;
while ((D=raf.read ())!=-1) {
Fout.write (d);
Fout.flush ();
}
Fout.close ();
In.close ();
Out.close ();
Raf.close ();
File File=new file (filepath+ "Temp.txt". Replace ("\" "," "));
if (file.exists ()) {
if (File.delete ()) {
This.log ("finished");
}
}
}
}
Run again, well, there is no problem, but the bigger problem comes out, I upload pictures of what the small file is OK, my hand is a cheap upload of a multi-g movie, then the fan rang, and then the CPU use rate to 95%, then I have two eyes a black, wash and sleep ...
About the Request.getinputstream method uploading a file