Struts2 单个文件上传/多文件上传

1导入struts2-blank.war所有jar包:\struts-2.3.4\apps\struts2-blank.war

单个文件上传

upload.jsp

<s:form action="upload2.action" method="post" theme="simple" enctype="multipart/form-data">
<tr>
<td id="more">
选择上传文件:<s:file name="file"></s:file><br>
<s:submit type="button" value="submit"/>
</td>
</tr>
</s:form>

struts.xml

 <package name="struts2" extends="struts-default">
<action name="upload2"class="com.hloytax.wg.upload.UploadAction1">
<result name="success">/success.jsp</result>
<interceptor-ref name="fileUpload">
<param name="maximumSize">409600</param> //上传文件大小设置
// <!--allowedTypes (可选) - 以逗号分割的contentType类型列表(例如text/html), <param name="contentType"> application/txt; </param>
<param name="allowedTypes">
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</action>
</package>
UploadAction1.action
public class UploadAction1 extends ActionSupport {

    /**
*
*/ private static final long serialVersionUID = 1L; private File file;
//文件名称
private String fileFileName; //文件类型
private String fileContentType;
//注意:文件名称和文件类型的名称前缀必须相同,
省略get set 方法 @Override
public String execute() throws Exception{ //获取需要上传文件的文件路径
File uploadFile=new File(ServletActionContext.getServletContext().getRealPath("uploadFile"));
//判断文件是否上传,如果上传的话将会创建该目录
if(!uploadFile.exists()){
uploadFile.mkdir(); //创建该目录
} /*//第一种文件上传的方法
//声明文件输入流,为输入流指定文件路径
FileInputStream input=new FileInputStream(file);
//获取输出流,获取文件的文件地址及名称
FileOutputStream out=new FileOutputStream(uploadFile + "\\" +fileFileName); try{
byte[] b=new byte[1024];//每次写入的大小
int i=0;
while((i=input.read(b))>0){
out.write(b,0,i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
input.close();
out.close();
}
*/ //第二种文件上传的方法
//FileUtils.copyFile(file,new File(uploadFile+"\\"+fileFileName));
// FileUtils.copyFile(file,new File(uploadFile,fileFileName));
// System.out.println(uploadFile); //第三种方法
BufferedReader bReader=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
BufferedWriter bWriter=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadFile+"\\"+fileFileName)));
System.out.println(uploadFile);
try{
char[] str=new char[1024];
int i=0;
while((i=bReader.read(str))>0){
bWriter.write(str,0,i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
bReader.close();
bWriter.close();
uploadFile.delete();
} return SUCCESS;
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

多文件上传:

private List<File> file;
private List<String> fileContentType;
private List<String> fileFileName;
private String savePath;
省略get set 方法 上传方法 参照单文件上传
@Override
    public String execute() throws Exception {
        List<File> files= getFile();
        if (files !=null) {
                for (int i = 0; i < files.size(); i++) {
                    FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getFileFileName().get(i));
                    //建立上传文件的输入流
                    System.out.println(getSavePath());
                    FileInputStream fis = new FileInputStream(files.get(i));         byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = fis.read(buffer)) > 0) {
             fos.write(buffer, 0, len);
         }
         fis.close();
         fos.close();
     }
 }
      return SUCCESS;
        
            }     /**
     * 返回上传文件保存的位置
     *
     * @return
     * @throws Exception
     */
    public String getSavePath() throws Exception {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }
    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }
上一篇:Fools and Roads CodeForces - 191C


下一篇:题解 CF191C 【Fools and Roads】