直接上代码
jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
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>上传文件</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>
<form action="<%=path %>/fileUp/fileUp.action" method="post" enctype="multipart/form-data">
<input type="file" name="picture"><br/>
<input type="submit" value="提交">
</form>
</body>
</html>
java
package com.demo.fileup;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 本类用于测试文件上传到服务器的功能
*
* @author WuJieJecket
*
* 1、jsp:fileUp.jsp 2、struts:struts-fileup.xml
* 3、action:FileUpAction.java
*
*/
@SuppressWarnings("serial")
public class FileUpAction extends ActionSupport {
// 注意,file并不是指前端jsp上传过来的文件本身,而是文件上传过来存放在临时文件夹下面的文件
private File picture;
// 提交过来的file的名字
private String pictureFileName;
// 提交过来的file的MIME类型
private String pictureContentType;
public String toFileUp() {
return "success";
}
/**
* 文件上传
* @return
* @throws IOException
*/
public String fileUp() throws IOException {
final String root="/tmp/share";//绝对路径,从根目录开始-linux环境
final String root1="d:/a";//绝对路径,从根目录开始-windows环境
final String root2=ServletActionContext.getServletContext().getRealPath("upload");//相对路径,从项目webapp目录下开始
File saved=new File(root,pictureFileName);//最终将文件保存到/upload下
InputStream ins=null; //文件输出流
OutputStream ous=null; //文件输出流
try{
saved.getParentFile().mkdir(); //确保文件夹/upload存在
ins=new FileInputStream(picture); //读入临时文件
ous=new FileOutputStream(saved); //写入到upload下
byte[] b=new byte[1024]; //字节缓存
int len=0;
while((len=ins.read(b))!=-1){ //循环读入,直至结束
ous.write(b, 0, len); //写入文件
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(ous!=null)ous.close(); //关闭流
if(ins!=null)ins.close(); //关闭流
}
return "success";
}
public File getPicture() {
return picture;
}
public void setPicture(File picture) {
this.picture = picture;
}
public String getPictureFileName() {
return pictureFileName;
}
public void setPictureFileName(String pictureFileName) {
this.pictureFileName = pictureFileName;
}
public String getPictureContentType() {
return pictureContentType;
}
public void setPictureContentType(String pictureContentType) {
this.pictureContentType = pictureContentType;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!--struts-default-->
<!-- 设置文件临时存储位置 -->
<constant name="struts.multipart.saveDir" value="upload"/>
<package name="fileUp" extends="struts-default" namespace="/fileUp">
<!-- 跳转到文件上传页面
http://localhost:8080/struts2go/fileUp/toFileUp.action
-->
<action name="toFileUp" class="com.demo.fileup.FileUpAction" method="toFileUp">
<result name="success">/fileUp/toFileUp.jsp</result>
</action>
<!--处理图片上传请求 -->
<action name="fileUp" class="com.demo.fileup.FileUpAction" method="fileUp">
<result name="success">/fileUp/fileUp.jsp</result>
</action>
</package>
</struts>
默认情况下,struts2下载文件是有大小限制的,只有2M
如果想提高上限,在struts.xml中做如下配置,单位是字节-这个是全局的
<!-- 设置文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="1073741824"/><!-- 1G大小,1073741824字节 -->