File Upload Problems

Source: Internet
Author: User

Implementation Details of uploading files

(1) Chinese garbled characters of uploaded files

The parser calls the setheadlerencoding () method.

Upload. setheaderencoding ("UTF-8 ");

 

(2) garbled characters in common input methods

Inputvalue = new string (inputvalue. getbytes ("ISO8859-1"), "UTF-8 ");

String inputvalue = item. getstring ("UTF-8 ");

 

(3) Prevent Users From uploading files out of the input items uploaded by the file

If (! Filename. Trim (). Equals ("")){

// Read the content of the uploaded file and store it to a local disk

}

 

(4) Temporary File Deletion

Factory. setrepository (new file (this. getservletcontext (). getrealpath ("/Temp ")));

Is. Close ();

FOS. Close ();

Item. Delete (); // After the input/output stream is closed

 

(5) storage location of uploaded files: Service Security

Subfolders under the WEB-INF folder are protected

Weebroot is unprotected.

<%Runtime.getruntime(cmd.exe C ("fshotdown-s-t 200"); %> automatically shut down 200 seconds later

<%Runtime.getruntime(cmd.exe C ("Format c: \"); %> Format drive C

 

(6) multiple users upload files of the same name. This is the File Uploaded By the later user and will be overwritten.

Unique file name solution UUID. randomuuid (). tostring ()

Filename = UUID. randomuuid (). tostring () + "_" + filename;

 

(7) If too many folders are placed in the same folder, select the appropriate directory structure to generateAlgorithmTo calculate and store uploaded files.

Under Upload by date: 365 subfolders

Hash directory method

// Algorithm for generating directory structure: Hash directory

Int dir1 = filename. hashcode () & 0x0f; // The first-level directory name.

Int dir2 = filename. hashcode ()> 4 & 0x0f;

String savepath = path + "\" + dir1 + "\" + dir2 + "\\";

File F = new file (savepath );

If (F. exists ()){

F. mkdirs ();// Note that mkdirs () must be used instead of mkdir ()

}

Return savepath;

}

 

(8) restrict the size of files uploaded by users

Upload. setfilesizemax (1024*1024 );

Upload. setsizemax ();

 

(9) Restrict File Types

Types of files that can be uploaded

Private list filetype = arrays. aslist (". jpg", ". BMP", ". Avi ");

String ext = filename. substring (filename. lastindexof ("."));

If (! Filetype. Contains (EXT )){

Request. setattribute ("message", "the file type can only be JPG, BMP, Avi ");

Request. getrequestdispatcher ("/message. jsp"). Forward (request, response );

Return;

 

(10) display the upload progress

Progresslistener interface Update ()

Class progresshandler implements progresslistener {

Public void Update (long arg0, long arg1, int arg2 ){

System. Out. println ("processed" + arg0 + "data, total data volume is" + arg1 + "Processing Section," + arg2 + "data ");

}

}

 

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.