struts2上傳檔案

來源:互聯網
上載者:User

標籤:

java代碼:

package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

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

import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport {
private File upload;
private String uploadFileName;
private String uploadContentType;

public String doUpload() throws IOException{
/*System.out.println(upload);
System.out.println(uploadContentType);
System.out.println(uploadFileName);*/
String savePath = ServletActionContext
.getServletContext()
.getRealPath("/upload/"+this.uploadFileName);
System.out.println(savePath);
FileInputStream fis = new FileInputStream(upload);
FileOutputStream fos = new FileOutputStream(savePath);
IOUtils.copy(fis, fos);
fos.flush();
fos.close();
fis.close();

return SUCCESS;
}

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;
}
}

 

 

Struts2.xml配置:

<?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="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.ui.theme" value="simple"/>
<constant name="struts.multipart.maxSize" value="1000000" />
<constant name="struts.i18n.encoding" value="utf-8"/>

<package name="default" namespace="/" extends="struts-default">
<action name="doUpload" class="action.UploadAction"
method="doUpload">
<interceptor-ref name="fileUpload">
<param name="maximumSize">1000000</param>
<param name="allowedExtensions">jpg,jpeg,gif,png</param><!-- 也可以使用allowedTypes來控制上傳的檔案類型 -->
</interceptor-ref>
<interceptor-ref name="defaultStack"/>
<result>/singleSuccess.jsp</result>
<result name="input">/error.jsp</result>
</action>
</package>

</struts>

 

jsp:


<form action="doUpload" method="post"
enctype="multipart/form-data">
請選擇檔案:<input type="file" name="upload">
<input type="submit" value="上傳檔案">
</form>

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.