Struts file upload and struts2 File Upload
Use two packages:One is the commons-fileupload-1.3.2.jar, the other is the commons-io-2.2.jar
Jsp interface:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Do Action.
Three members: 1. File file uploaded File 2. String fileFileName attribute name 3. String fileContentType attribute name
Org. apache. commons. io. FileUtils. copyFile is simple.
Sturts. xml configuration file. You can add constants in it to modify the file upload size limit. You can upload large files.
<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE struts PUBLIC "-// Apache Software Foundation // DTD Struts Configuration 2.3 // EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <constant name = "struts. enable. dynamicMethodInvocation "value =" true "/> <constant name =" struts. devMode "value =" true "/> <constant name =" struts. i18n. encoding "value =" UTF-8 "> </constant> <constant name =" struts. ui. theme "value =" simple "> </constant> <constant name = "Struts. multipart. maxSize" value = "20971520"> </constant> <! -- Modify the limit on the size of the uploaded file, default Value: 2097152 --> <package name = "default" namespace = "/" extends = "struts-default"> <action name = "* _ *" class = "com. itnba. maya. controller. {1} Action "method =" {2} "> <result> {1 }_{ 2 }. jsp </result> </action> </package> <include file = "example. xml "/> <! -- Add packages here --> </struts>
Rename
Package com. itnba. maya. controller; import java. io. file; import java. io. IOException; import java. text. simpleDateFormat; import java. util. date; import org. apache. commons. io. fileUtils; import com. opensymphony. xwork2.ActionSupport; public class ShangchuanAction extends ActionSupport {private File file; private String fileFileName; // name of the uploaded File private String fileContentType; // format of the uploaded File public file getFile () {return file;} public String getFileFileName () {return fileFileName;} public void setFileFileName (String fileFileName) {this. fileFileName = fileFileName;} public String getFileContentType () {return fileContentType;} public void setFileContentType (String fileContentType) {this. fileContentType = fileContentType;} public void setFile (File file) {this. file = file;} public String input () {return SUCCESS;} public String add () throws IOException {// to prevent duplicate names of uploaded files, add the current time long l = System before the file. currentTimeMillis (); Date date = new Date (l); SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMddhhmmssSSS"); // precise to millisecond String pre = sdf. format (date); String fileName = pre + fileFileName; File destFile = new File ("d: \" + fileName); // path where the File is stored + File name FileUtils. copyFile (file, destFile); return SUCCESS ;}}
Uploaded
**************************************** **************************************** *****
Multiple files are uploaded at the same time. The three members are defined as arrays.