Simple FileUpload file upload and fileupload File Upload

Source: Internet
Author: User

Simple FileUpload file upload and fileupload File Upload

Introduce jar package: commons-fileupload-1.3.1.jar, commons-io-1.3.2.jar

Foreground HTML:

<Form action = "./upload" method = "post" enctype = "multipart/form-data">
<Table>
<Tr>
<Td> <label> User name: </label> </td>
<Td> <input type = "text" name = "userName"/> </td>
</Tr>
<Tr>
<Td> <label> password: </label> </td>
<Td> <input type = "text" name = "userPwd"/> </td>
</Tr>
<Tr>
<Td> <label> file name: </label> </td>
<Td> <input type = "file" name = "fileName1"/> </td>
</Tr>
<Tr>
<Td> <label> file name: </label> </td>
<Td> <input type = "file" name = "fileName2"/> </td>
</Tr>
<Tr>
<Td> <input type = "submit" value = "submit"/> </td>
<Td> <input type = "reset" value = "reset"/> </td>
</Tr>
</Table>
</Form>

Servlet configured in the background:

Public class FileUpload extends HttpServlet {
Private String uploadPath = "C: \ upload \"; // directory of the uploaded file

@ Override
Protected void doGet (HttpServletRequest req, HttpServletResponse resp)
Throws ServletException, IOException {
Super. doGet (req, resp );
}

@ Override
Protected void doPost (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

Try {
// Determine whether a file is uploaded
Boolean isMultiPart = ServletFileUpload. isMultipartContent (request );
If (isMultiPart ){
DiskFileItemFactory factory = new DiskFileItemFactory ();
// Set factory constraints
// Factory. setSizeThreshold (yourMaxMemorySize );
// Factory. setRepository (yourTempDirectory );
ServletContext context = this. getServletConfig ()
. GetServletContext ();
File repository = (File) context
. GetAttribute ("javax. servlet. context. tempdir ");
Factory. setRepository (repository );
ServletFileUpload upload = new ServletFileUpload (factory );
// Set overall request size constraint
// Upload. setSizeMax (yourMaxRequestSize );
List <FileItem> items = upload. parseRequest (request );
Iterator <FileItem> iter = items. iterator ();
While (iter. hasNext ()){
FileItem item = iter. next ();

If (item. isFormField ()){
String name = item. getFieldName ();
String value = item. getString ();
System. out. println (name + ":" + value );
} Else {
String fieldName = item. getFieldName ();
String fileName = item. getName ();
String contentType = item. getContentType ();
Boolean isInMemory = item. isInMemory ();
Long sizeInBytes = item. getSize ();

System. out. println ("fieldName:" + fieldName );
System. out. println ("fileName:" + fileName );
System. out. println ("contentType:" + contentType );
System. out. println ("isInMemory:" + isInMemory );
System. out. println ("sizeInBytes:" + sizeInBytes );

File uploadedFile = new File (uploadPath + System. currentTimeMillis () + ". txt ");
Item. write (uploadedFile );

// InputStream uploadedStream = item. getInputStream ();
//...
// UploadedStream. close ();
// Process a file upload in memory
// Byte [] data = item. get ();
}
}
}
} Catch (Exception e ){
E. printStackTrace ();
}
}

}

Web. xml file Configuration:

<Servlet>
<Servlet-name> FileUpload </servlet-name>
<Servlet-class> com. jt. fileupload. FileUpload </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> FileUpload </servlet-name>
<Url-pattern>/upload </url-pattern>
</Servlet-mapping>


Simple backend code for uploading files in ASPNET

I have my own website file management program, which includes functions such as file deletion, upload, rename, and decompression. I will send you an email.
Below is some code
Private string DoFileUpload ()
{
String strResult = "";
If (upFile. PostedFile! = Null & upFile. PostedFile. FileName! = Null
& Amp; upFile. PostedFile. FileName! = String. Empty)
{
String FileName = upFile. PostedFile. FileName;
String LocalFileName;
String split = @"\/";
Char [] anyOf = split. ToCharArray ();
String [] PathParts = FileName. Split (anyOf );
If (PathParts. Length> 0)
{
LocalFileName = sPhyRoot + "\" + sCurRelDir + "\" + PathParts [PathParts. Length-1];
UpFile. PostedFile. SaveAs (LocalFileName );
StrResult = LocalFileName;
}
}
Return strResult;
}

How to upload and download files using io streams without simple file upload and download frameworks

This requires you to be familiar with the HTTP protocol. If you don't want to use the framework, you can use the apache File Upload Component. Baidu searches for commons-fileUpload.
 

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.