One of the JSP file upload and download series [basic file upload methods]

Source: Internet
Author: User
Preface

In JSP web projects, the file upload function is likely to be used.

Upload a local file to a path on the Web server.

For Java, there are many open-source components that implement such functions, such as commons-fileupload, and so on ....

However, the underlying principles of components should be consistent. Therefore, this series starts with the basic methods and principles.

Select the file button tag

<Input type = "file"/>
This is an HTML Tag, so the methods used by ASP, JSP, and PHP are the same.
This label is placed in the form label. The "select file" button is displayed. A file selection dialog box is displayed.

This label may display different effects in different runners (Firefox and chrome have similar effects, but ie shows different effects. )

To add a file tag to a form, set form method to "Post" and enctype to "multipart/form-Data". In this case, when you click Submit,

The file content is put into the request object of the page as a data stream.

The configuration is similar:

<form action="BaseUpload.jsp" method="post" enctype="multipart/form-data">Please Select File: <input type="file" name="upload" size="16"><br><input type="submit" value="submit"></form>

Action configuration in Form

Action is used to process the data in the form and what is returned after processing.

For JSP, action can be directly configured as a. jsp file or a servlet. If spring is used, it can also be a. Do or link file.

For convenience, the. jsp method is used directly in the instance.

Instance

1. Eclipse is used for development. Therefore, a dynamic web project is created through eclipse.

2. Create a file in the webcontent directory and select the file page baseuploadfile. jsp (the suffix can also be .html)

<!-- Add By oscar999 --><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

2. Define the JSP baseupload. jsp of the action in the same directory.

<%@ page contentType="text/html;charset=UTF-8" %><%@ page import="java.io.*"  %><%String sFileName = "tempfile.txt";String sFilePath = "D:\\temp\\upload\\files\\";ServletInputStream in = request.getInputStream();//FileOutputStream outputStream = new FileOutputStream(sFilePath+sFileName);PrintWriter pw = new PrintWriter( new BufferedWriter(new FileWriter(sFilePath+sFileName))); int i = in.read();while(i!=-1){pw.print((char)i);i = in.read();}pw.close();in.close();%>

The action here is very simple. It is to write the request information to a TXT file to verify whether the file content is put into the request object as previously mentioned.

3. Package and deploy. Test. View the generated TXT file content

Upload a simple TXT text such as "111. The content in the generated file is as follows:

------WebKitFormBoundaryGsj8EiAn9BkPrhAfContent-Disposition: form-data; name="upload"; filename="111.txt"Content-Type: text/plain111------WebKitFormBoundaryGsj8EiAn9BkPrhAf--

Therefore, you can get the content of the uploaded file from the request and write it to the server through the stream.

Supplement

1. For baseupload. jsp of the preceding actions, you can also use the following methods:

Define a processing Java Bean, baseuploadbean. Java

/**    * @author oscar999    * @date 2013-8-7 * @version V1.0    */package com.oscar999;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServletRequest;public class BaseUploadBean {public void doUpload(HttpServletRequest request) throwsIOException {String sFileName = "D:\\temp\\upload\\files\\tempfile.txt";PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(sFileName)));ServletInputStream in = request.getInputStream();int i = in.read();while (i != -1) {pw.print((char) i);i = in.read();}pw.close();in.close();}}

In this way, the content of baseupload. jsp can be simplified:

<% @ Page contenttype = "text/html; charset = UTF-8" %>
<% @ Page import = "Java. Io. *" %>
<JSP: usebean id = "thebean" Scope = "page" class = "com. oscar999.baseuploadbean"/>
<%
Thebean. doupload (request );
%>

2. In addition, Action uses Servlet

Configure servlet in Web. xml

  <servlet>  <servlet-name>FileUploadServlet</servlet-name>  <servlet-class>com.oscar999.FileUploadServlet</servlet-class>  </servlet>      <servlet-mapping>  <servlet-name>FileUploadServlet</servlet-name>  <url-pattern>/FileUploadServlet</url-pattern>  </servlet-mapping>  

Then perform similar processing in the dopost method.

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.