Struts2 上傳檔案

來源:互聯網
上載者:User

jsp page code:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'array.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body>
  <div>數組參數測試</div>
    <form action="array.action" enctype="multipart/form-data" method="post">
    <input type="text" name="userName"/>
    <input type="text" name="userName"/>
    <input type="text" name="userName"/>
    <input type="text" name="userName"/>
    <input type="text" name="userName"/>
    <input type="file" name="data">
    <input type="submit" value="提交資料">
    </form>
  </body>
</html>

Struts2 Action code:

package com.action;
import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class ArrayAction{

private String [] userName;
private File data;
private String dataFileName;
private String dataContentType;


public void setDataContentType(String dataContentType) {
this.dataContentType = dataContentType;
}

public void setDataFileName(String dataFileName) {
this.dataFileName = dataFileName;
}

public void setData(File data) {
this.data = data;
}

public void setUserName(String[] userName) {
this.userName = userName;
}

/**
* @return
*/
public String execute() {
// TODO Auto-generated method stub
System.out.println("ok! function!");
//輸出數組中的資料
for (String str : userName) {
System.out.println(str);
}
//檔案處理
try {
String realpath = ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(dataFileName+"檔案的名稱");
System.out.println(dataContentType+"檔案的類型");
 File savefile = new File(new File(realpath), dataFileName);
 System.out.println(savefile.getPath());
 FileUtils.copyFile(data, savefile);
 System.out.println("檔案上傳成功!");
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
// File saveFile=new File(this.file,file.getName());

return "success";
}
}

struts.xml config file:

<?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.i18n.encoding" value="UTF-8"/>       <package name="default" extends="struts-default">            <action name="login" class="com.action.LoginAction">          <result name="success">/success.jsp</result>        </action>                <action name="reg" class="com.action.LoginAction" method="reg">          <result name="success">/success.jsp</result>        </action>        <action name="array" class="com.action.ArrayAction">        <result name="success">/success.jsp</result>        </action>               </package>     <!-- Add packages here --></struts>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.