JSP uploads files and produces examples of high-definition thumbnails

Source: Internet
Author: User

Preface

At the beginning, I only wanted to test the effect of Thumbnails generating Thumbnails. By the way, I learned about the jsp file. I didn't use the commons-fileupload Upload Component and wrote it myself using pure jsp code, the process is rather tortuous. Therefore, it is not recommended that you compile the JSP-only upload code. For details, refer to the source code of commons-fileupload.

I. JSP file upload

As we all know, files are uploaded in binary format, so that files can be uploaded. Therefore, JSP must upload files in binary format, we need to set enctype = "multipart/form-data" when submitting an HTML form, which should be clear to everyone.

First, I will first list the jar package references. First, we will find these jar files and introduce them to the project.

Commons-fileupload-1.2.2.jar


Commons-io-2.0.1.jar


Thumbnailator-0.4.2.jar


First, upload the JSP code of the page. In fact, it is very easy to put a file selection box. I added a text box for testing.

Index. jsp

 

<% @ Page contentType = "text/html" pageEncoding = "UTF-8" %> <! DOCTYPE html> 
<% @ Page import = "net. coobird. thumbnailator. thumbnails "%> <% @ page import =" org. apache. commons. fileupload. fileItem "%> <% @ page import =" java. util. iterator "%> <% @ page import =" java. util. hashtable "%> <% @ page import =" java. util. map "%> <% @ page import =" org. apache. commons. fileupload. fileUploadException "%> <% @ page import =" java. util. list "%> <% @ page import =" org. apache. commons. fileupload. disk. diskFileItemFactory "%> <% @ page Import = "org. apache. commons. fileupload. fileItemFactory "%> <% @ page import =" org. apache. commons. fileupload. servlet. servletFileUpload "%> <% @ page import =" java. io. file "%> <% @ page contentType =" text/html "pageEncoding =" UTF-8 "%> <! DOCTYPE html> 


Let me briefly describe the code. We use ServletFileUpload. isMultipartContent (request) is used to determine whether a user's form is uploaded in binary format, thus changing the mode of obtaining the submitted form data. getParameter cannot obtain the value.

By using commons-fileupload, we can easily get the user form Upload File stream and keep it to our server disk.

In this example, if a thumbnail is generated for a test image, the uploaded file type is not considered. I will upload the image type. If other types of files are uploaded, the page will be abnormal, however, files can be uploaded.

Let's take a look at the code for generating a thumbnail, just a simple line of code.

 

// Size (width, height)/** if the image horizontal ratio is smaller than 200, the height ratio is smaller than 300. * If the image horizontal ratio is smaller than 200, the height ratio is greater than 300, and the height is reduced to 300, the image proportion remains unchanged * If the image horizontal ratio is greater than 200, the aspect ratio is smaller than 300, and the aspect ratio is reduced to 200. * If the image horizontal ratio is greater than 200, the aspect ratio is greater than 300, and the image proportion is reduced, the horizontal value is 200 or the height is 300 */Thumbnails. of ("c:/images/1280x1024.jpg "). size (200,300 ). toFile ("c:/200x300.jpg ");

 

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.