File Upload function implementation (ii)

Source: Internet
Author: User

IE Low version of file name Bug resolution

Questions:UseFileitem.getname ()This method of time,can get the file name, General browser to get the format: Xx.png

But what IE gets is d:/xx/xx/xx.png our function will not be realized

Solution: Using the import in IO package

The GetName () method in the Filenameutils tool class can be obtained xx.png

So: Let's change the code to get the name.

Pre-modified ie ) :

String fileName =fileitem.getname ();

Modified ( solved ie ) :

String fileName =  Filenameuti ls. getName ( fileitem.getname () ) ;

         

② the name of the uploaded file has conflicting issues

Problem: If there is a file with the same name, it will fail to upload or replace the original file

Solution: Change the name of the uploaded file to a random number (note: The suffix is immutable)

Code Analysis:

get the name of the file

String fileName = filenameutils. GetName (Fileitem.getname ());

get the suffix of the file

String extname = filenameutils. getextension (fileName);

generates a random name, which can be a time string

Date Date=new date (); String upname=string.valueof (date);

String fileuuidname = UUID. Randomuuid (). toString ();

combined into our random name ( with suffix )

String randomfilename = fileuuidname+ "." +extname;

location of last file upload

File UploadFile = new file ("d:/", randomfilename);

Note:When stitching the file name, be sure not to forget "."     

③ Resolving file Save location issues

Problem: Now my file is saved by disk, and some operating systems do not have a drive letter.

Solution: Direct Save the file to our project you can do it.

Get the root path of the current project (the actual path) through the context object to get

Super. Getservletcontext (). Getrealpath ("/"); parentheses can write the path we want to save in the file.

But you must have this folder under your project (I created a upload folder)

Code: Get the path to this project

location of last file upload

String Realpath = Super. Getservletcontext (). Getrealpath ("/upload");

File UploadFile = new file (realpath,randomfilename);

If a problem with the path setting throws an exception that the file path cannot be found: check if there is a path under your project

Code:

@WebServlet ("/upload")
public class Uploadservlet extends HttpServlet {
@Override
protected void Service (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, IOException {
Create a factory to parse this request, get a collection, this collection is a request object, encapsulated is each request data
try {
Create a factory
Fileitemfactory factory = new Diskfileitemfactory ();

Create a File Upload processor
Servletfileupload upload = new Servletfileupload (factory);

Parse this request: After parsing, we can get a collection of Fileitem.
List < fileitem > items = upload.parserequest (req);

By iterating through the collection you can get the property value of the uploaded file, including the property name, value.
for (Fileitem Fileitem:items) {
The above tests can look at the output, and the normal and file controls are treated differently.
if (Fileitem.isformfield ()) {
Representative is the general form, with the general form of the processing method to solve
String username = req.getparameter ("username");
String Password = req.getparameter ("password");

}else{
Get the name of the uploaded file (with IE version issue)
String Upname=fileitem.getname ();
Solution Solutions
String upname = Filenameutils.getname (Fileitem.getname ());


Solve problems with duplicate file uploads
① get the name of the uploaded file
String upname = Filenameutils.getname (Fileitem.getname ());
② get the suffix name of the uploaded file (using the Filenameutils tool Class)
String extname = filenameutils.getextension (upname);
③ generates a random name that provides two ways
String fileuuidname = Uuid.randomuuid (). toString ();


Date Date=new date ();

String str=string.valueof (date);


Stitching into a new file name as a saved name after the file is uploaded
String upfilename=fileuuidname+ "." +extname;

Solve the problem of saving road path
① get the path of the uploaded file saved in the project
String Realpath = Super.getservletcontext (). Getrealpath ("/upload");
System.out.println (Realpath);
Determine where files are uploaded
File UploadFile = new file (realpath,upfilename);

Write local files to the server
Fileitem.write (UploadFile);
}
}
} catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

I am also a beginner, just put some of my understanding on the above, there are a lot of shortcomings, please give some advice!

File upload function implementation (b)

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.