Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize File upload function by java components SmartUpload and FileUpload

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "java components SmartUpload and FileUpload how to achieve file upload function", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "java components SmartUpload and FileUpload how to achieve file upload function" it!

1 SmartUpload upload component

SmartUpload upload component package, which can easily upload and download files.

Easy to use, realize the restriction on the type of uploaded file, and easily get the name, suffix, size and so on of the uploaded file.

SmartUpload itself is a jar package provided by the system. Put this package into the lib folder.

The provider website for this component has been closed, and SmartUpload is easier to use in non-framework development.

Upload a single file

To upload, the file space provided in HTML must be used and must be encapsulated with the enctype attribute

Smartupload_demo01.html: upload form

To upload the form, please select the file to upload:

The form is encapsulated with enctype on form, indicating that the form will be submitted in binary mode, that is, all forms will not be submitted separately at this time, but all the content will be submitted in binary mode.

Smartupload_demo01.jsp: receive pictures and save them in the upload folder in the root directory

Receive the picture and save it in the upload folder in the root directory

When using SmartUpload, you must strictly follow the above procedure. Finally, when saving, you only write a upload to indicate the folder where the uploaded file is saved. This file should be created manually in the root directory.

The saved name is the same as the uploaded file, so if the same file name appears, it will be overwritten.

Mixed form

When a form uses enctyoe encapsulation, the contents of form controls of other file classes cannot be obtained through request built-in objects

At this point, all the request parameters must be obtained through the getRequest () method provided in the SmartUpload class.

Smartupload_demo02.html; mixed form

Mixed form name:

Photo:

The above form contains two controls, text and file.

Smartupload_demo02.jsp: receives text data that encapsulates the form

Receive the text data name that encapsulates the form: request cannot get:

The form is encapsulated in binary. The submission parameters cannot be obtained by request object alone, and the requested parameters can only be obtained by relying on the getRequest (). GetParameter () method in the SmartUpload class.

Since parameter reception is done through SmartUpload, the smart.getRequest () method cannot be used until the upload () method is executed.

Automatic naming for uploaded files

In order to solve the problem of overwriting due to the same file name, you can use automatic naming for uploaded files.

Automatic naming can be in the format of IP address + timestamp + three-digit random number

IPTimeStamp.java: defines the operation class that acquires the IP timestamp

Package cn.com.bug.util; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; public class IPTimeStamp {private SimpleDateFormat sdf = null; / / define SimpleDateFormat object private String ip = null; / / receive IP address public IPTimeStamp () {} public IPTimeStamp (String ip) {/ / get IP address + timestamp + three-digit random number this.ip = ip;} public String getIPTimeRand () {StringBuffer buf = new StringBuffer () / / instantiate StringBuffer object if (this.ip! = null) {String s [] = this.ip.split ("\\."); / / perform split operation for (common parameters of int iTunes:

In the above code, all uploaded files are first set to a temporary folder

If you find that the obtained form is an uploaded file, use InputStream to get the input stream of the file from the FileItem class

After using OutputStream, the contents are taken out in turn and saved in the specific file path.

Inconvenience of FileUpload under construction:

1 cannot get the submitted parameters as accurately as using the request.getParameter () method

2 A set of submission parameters cannot be obtained as accurately as using request.getParameterValues ()

3 all uploaded files need to be judged once before they can be saved separately, not in batches at one time.

Thank you for your reading, the above is the "java components SmartUpload and FileUpload how to achieve file upload function" of the content, after the study of this article, I believe you on the java components SmartUpload and FileUpload how to achieve file upload function of this problem has a deeper understanding, the specific use of the need for everyone to practice and verify. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report