struts2 最基本的文件上传(struts2)

package cn.xxx.action;

import java.io.File;

import org.apache.commons.io.FilenameUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class FileUploadAction  extends ActionSupport{
	
		private File file;
	    private String fileFileName;
	    private String fileFileContentType;


	    public File getFile() {
	        return file;
	    }

	    public void setFile(File file) {
	        this.file = file;
	    }

	    public String getFileFileName() {
	        return fileFileName;
	    }

	    public void setFileFileName(String fileFileName) {
	        this.fileFileName = fileFileName;
	    }

	    public String getFileFileContentType() {
	        return fileFileContentType;
	    }

	    public void setFileFileContentType(String fileFileContentType) {
	        this.fileFileContentType = fileFileContentType;
	    }

	    
	 public String upload(){
		
		        String path = ServletActionContext.getServletContext().getRealPath("");//项目发布路径
		        
		        path = path +"\\upload\\"+"images";
		        String ext = FilenameUtils.getExtension(fileFileName); //得到后缀
		        file.renameTo(new File(path, "test"+"."+ext)); //把上传的文件存到path路径下
		        System.out.println("path:"+path);
		      
		        return null;
		 
	 }


}

<s:form action="fileUpload_upload" method="post" enctype="multipart/form-data">
	<input type="file" name="file">
	<input type="submit" value="提交" />
</s:form>

这样就可以上传了,当然还要jar包。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值