struts2-上傳檔案

來源:互聯網
上載者:User

標籤:name   public   leo   www   output   lan   this   圖片   add   

package web;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import org.apache.commons.io.FileUtils;import org.apache.commons.io.IOUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadDemoAction extends ActionSupport {    private String username;    private File myfile;    private String myfileContentType;    private String myfileFileName;    public String getUsername() {        return username;    }    public void setUsername(String username) {        this.username = username;    }    public File getMyfile() {        return myfile;    }    public void setMyfile(File myfile) {        this.myfile = myfile;    }    public String getMyfileContentType() {        return myfileContentType;    }    public void setMyfileContentType(String myfileContentType) {        this.myfileContentType = myfileContentType;    }    public String getMyfileFileName() {        return myfileFileName;    }    public void setMyfileFileName(String myfileFileName) {        this.myfileFileName = myfileFileName;    }    public String upload(){        try {            String path = ServletActionContext.getServletContext().getRealPath(                    "/upload/file");            System.out.println("path========"+path);            InputStream input = new FileInputStream(myfile);            System.out.println("myfileFileName===="+myfileFileName);            String allpath=path+"\\"+myfileFileName;            System.out.println("allpath===="+allpath);            OutputStream output = new FileOutputStream(allpath);            IOUtils.copy(input, output);            return SUCCESS;        } catch (Exception e) {            e.printStackTrace();            return INPUT;        }    }}

 

<%@ 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"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form action="upload.action" method="post" enctype="multipart/form-data">    使用者名稱:<input type="text" name="username"/>    檔案:<input type="file" name="myfile" />    <input type="submit" value="提交"/></form></body></html>
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts>    <!-- 忽略struts格式 -->    <constant name="struts.ui.theme" value="simple"></constant>    <constant name="struts.configuration.xml.reload" value="true"></constant>    <package name="houseuser" extends="struts-default" namespace="/">                <!-- 上傳圖片樣本-->        <action name="upload" class="web.UploadDemoAction" method="upload">            <result >success.jsp</result>        </action>    </package>    </struts>    

 

struts2-上傳檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.