In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the three ways of uploading struts2 files. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
File upload is a necessary module for almost every project.
Upload is to transfer information from a personal computer (local computer) to a central computer (remote computer) system so that people on the network can see it. Publish the produced web pages, text, pictures, etc., to the Internet for others to browse and appreciate. This process is called upload.
JAVA implements several components of file upload:
1 SmartUpload uses the most frequently used component, which is no longer updated and can be uploaded and downloaded.
2. The file upload component implemented by FileUpload Apache has complete functions.
3 File upload component implemented by J2KUpload java2000, all using memory, suitable for multiple small files no more than 10m
Let's talk about the file upload component implemented by FileUpload Apache in detail.
1. / * * upload by copy * /
Java code
Public String uploadFile () {/ * * saved specific path * / String savepath = getSavePath (); / * * create a file object based on the saved path * / File file = new File (savepath); if (! file.exists ()) {/ * * create this file object path * / file.mkdirs () } try {/ * * uses: org.apache.commons.io.FileUtils FileUtils*/ FileUtils.copyFile (pic, new File (file,getPicFileName ();} catch (IOException e) {e.printStackTrace ();} return SUCCESS;}
Note:
1. In the getSavePath () method, ServletActionContext (). GetServletContext (). GetRealPath
(savePath+ "\" + getPicFileName ()); this is mainly the actual path of a file
2. I personally think this method is easy to use. Uploaded by copy is made by Apache.
The FileUtils.java in the org.apache.commons.io.FileUtils bag.
2. / * upload by byte * /
Java code
Public String uploadFile () {/ * * File write operation * / FileInputStream fis = null; FileOutputStream fos = null; / * * saved path * / String savepath = getSavePath (); / * * create a file object based on the saved path * / File file = new File (savepath) / * * whether the file object exists * / if (! file.exists ()) {/ * * create this file object path * / file.mkdirs ();} try {/ * * create input stream * / fis = new FileInputStream (pic) / * * output stream creates file objects based on file path + file name * / fos = new FileOutputStream (file + "/ /" + getPicFileName ()); / * read bytes * / byte b [] = new byte [1024]; int n = 0 / * * read operation * / while ((n = fis.read (b))! =-1) {/ * * write operation * / fos.write (b, 0, n) } / * * close operation * / if (fis! = null) {fis.close ();} if (fos! = null) {fos.close ();}} catch (Exception e) {e.printStackTrace () } return SUCCESS;}
3. / * upload by character, that is, "three-tier pipeline" * /
Java code
Public String uploadFile () {/ * * File write operation * / BufferedReader br = null; BufferedWriter bw = null; / * * saved path * / String savepath = getSavePath (); / * * create a file object based on the saved path * / File file = new File (savepath) / * * whether the file object exists * / if (! file.exists ()) {/ * * create this file object path * / file.mkdirs () } try {/ * * create a BufferedReader object * / br = new BufferedReader (new InputStreamReader (new FileInputStream (pic); bw = new BufferedWriter (new FileOutputStream (file + "/ /" + getPicFileName () / / read bytes char b [] = new char [1024]; int n = 0; / / read operation while ((n = br.read (b))! =-1) {/ / write operation bw.write (b, 0, n) } / / close operation if (br! = null) {br.close ();} if (bw! = null) {bw.close ();}} catch (Exception e) {e.printStackTrace () } return SUCCESS;}
Note:
The second upload method is not as efficient as the third upload method.
Recommendations:
First upload it in the first way, then upload it in the third way, and finally upload it in the second way.
These are the three ways of uploading struts2 files, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.