Use WebUploader to upload files (1 ),
Preface:
There are many File Upload methods. For large file uploads, this project also involves using multipart breakpoint to upload large files. So let's take a look at WebUploader. Let's start with simple file upload.
Write comments in the Code. If this is better, let's look at the code to understand the implementation process.
Front-end jsp page:
<% @ Page contentType = "text/html; charset = UTF-8" language = "java" %> <% String scheme = request. getScheme (); String serverName = request. getServerName (); String contextPath = request. getContextPath (); int port = request. getServerPort (); // The URL String baseURL = scheme + ": //" + serverName + ":" + port + contextPath; request. setAttribute ("baseURL", baseURL); %>
Background action:
/*** Description: com. ims. action * Author: Eleven * Date: */@ Controller ("FileAction ") public class FileAction extends BaseAction {// remember to provide the corresponding get set Method // upload a file object (same as the name value of form type = file). On the jsp page, we specify fileVal: 'upload',) private File upload; // File name private String uploadFileName; // upload type private String uploadContentType; public void uploadFile () throws Exception {String str = "D: /upload33/"; // file storage path: System. out. println ("File Path =" + uploadFileName); String realPath = str + uploadFileName; File tmp = new File (realPath); FileUtils. copyFile (upload, tmp); System. out. println ("upload File" + uploadFileName + ", size:" + (upload. length ()/1024/1024) + "M");} public File getUpload () {return upload;} public void setUpload (File upload) {this. upload = upload;} public String getUploadFileName () {return uploadFileName;} public void setUploadFileName (String uploadFileName) {this. uploadFileName = uploadFileName;} public String getUploadContentType () {return uploadContentType;} public void setUploadContentType (String uploadContentType) {this. uploadContentType = uploadContentType ;}}
Struts. xml file Configuration:
<action name="uploadFile" class="FileAction" method="uploadFile"> </action>
Now you can run it. This is only the most basic file upload implemented by WebUploader. In jsp pages and background actions, there is no logic or completeness. Because different projects and business processes are different, you can get started first and add them as needed.
Run:
Then, we will continue to organize the articles for multipart breakpoint upload.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.