struts2文件上传到服务器的某个文件夹下。
2、列表页
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>Struts2 File Upload</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/group!upload.action" method="POST" enctype="multipart/form-data">
选择文件:<input type="file" name="pic" size="50"/><br/>
<input type="submit" value=" 上传 "/>
</form>
</body>
</html>
|
3.action
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
public class FileUploadAction {
//上传用
private File pic;
private String picFileName;
private String picContentType;
private InputStream inputStream;
/**
* 功能:文件上传
* @author 闫加盼
* @return 跳转页面的字符串
* @throws IOException
* @throws ParseException
*/
public String upload(){
try {
//服务器的路径
String realpath = ServletActionContext.getServletContext().getRealPath("/shuju");
//例如-------D:\apache-tomcat-6.0.18\webapps\struts2_upload\images
System.out.println("上传到服务器的地址realpath: "+realpath);
//具体地址+文件名
String lj = realpath+"\\"+picFileName;
System.out.println("具体的地址lujing"+lj);
//列如------ D:\apache-tomcat-7.0.47\webapps\dyda\shuju\15636139999[通话详单]查询_1_1.xls
if (pic != null) {
File savefile = new File(new File(realpath), picFileName);
if (!savefile.getParentFile().exists())
savefile.getParentFile().mkdirs();
FileUtils.copyFile(pic, savefile);
ActionContext.getContext().put("message", "文件上传成功");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
public File getPic() {
return pic;
}
public void setPic(File pic) {
this.pic = pic;
}
public String getPicFileName() {
return picFileName;
}
public void setPicFileName(String picFileName) {
this.picFileName = picFileName;
}
public String getPicContentType() {
return picContentType;
}
public void setPicContentType(String picContentType) {
this.picContentType = picContentType;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
|
4.配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 该属性指定需要Struts2处理的请求后缀,该属性的默认值是action,即所有匹配*.action的请求都由Struts2处理。
如果用户需要指定多个请求后缀,则多个后缀之间以英文逗号(,)隔开。 -->
<constant name="struts.action.extension" value="action"/>
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭-->
<constant name="struts.serve.static.browserCache" value="false"/>
<!-- 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开-->
<constant name="struts.configuration.xml.reload" value="true"/>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息-->
<constant name="struts.devMode" value="true"/>
<!-- 默认的视图主题-->
<constant name="struts.ui.theme" value="simple"/>
<!--<constant name="struts.objectFactory" value="spring"/>-->
<!--解决乱码 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
<!-- 指定允许上传的文件最大字节数。默认值是2097152(2M)-->
<constant name="struts.multipart.maxSize" value="10701096"/>
<package name="upload" namespace="/upload"extends="struts-default">
<action name="group"class="com.ljq.action.UploadAction" method="upload()">
<result name="success">/WEB-INF/page/message.jsp</result>
</action>
</package>
</struts>
|
4.显示页面
<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>上传成功</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
</head>
<body>
上传成功!
<br/><br/>
<!-- ${pageContext.request.contextPath} tomcat部署路径,
如:D:\apache-tomcat-6.0.18\webapps\struts2_upload\-->
<img src="${pageContext.request.contextPath}/<s:property value="'images/'+imageFileName"/>">
<s:debug></s:debug>
</body>
</html>
|
| 前台file的name值必须和action里的值一样
http://www.cnblogs.com/linjiqin/archive/2011/03/21/1990674.html+keep_trying |