struts2的上传
2014-03-01 21:24
330 查看
第一种采用Servlet3.0的上传:
UploadServlet.java
package action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
//注释3.0版本使用
@MultipartConfig
public class UploadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Part part=request.getPart("file");
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
String fileName=request.getParameter("name");
out.println("上传文件的类型:"+part.getContentType()+"<br/>");
out.println("上传文件大小:"+part.getSize()+"<br/>");
Collection<String> headerNames=part.getHeaderNames();
for(String headerName:headerNames){
out.println(headerName+"---->"+part.getHeader(headerName)+"<br/>");
}
part.write(getServletContext().getRealPath("/uploadFiles")+"/"+fileName);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>action.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/uploadServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.jsp' starting page</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="/Uploads/uploadServlet" method="post" enctype="multipart/form-data">
文件名:<input type="text" name="name"/>
文件:<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
![](https://img-blog.csdn.net/20140301211751812?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFudGluZ21laQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](https://img-blog.csdn.net/20140301211816203?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFudGluZ21laQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
第二种采用struts2文件上传:
UploadAction.java
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String title;
private File uploadfile;
private String uploadfileContentType;
private String uploadfileFileName;
private String savePath;
public String upload() throws Exception {
FileInputStream fis = new FileInputStream(getUploadfile());
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadfileFileName());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
fos.close();
fis.close();
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUploadfile() {
return uploadfile;
}
public void setUploadfile(File uploadfile) {
this.uploadfile = uploadfile;
}
public String getUploadfileContentType() {
return uploadfileContentType;
}
public void setUploadfileContentType(String uploadfileContentType) {
this.uploadfileContentType = uploadfileContentType;
}
public String getUploadfileFileName() {
return uploadfileFileName;
}
public void setUploadfileFileName(String uploadfileFileName) {
this.uploadfileFileName = uploadfileFileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);//得到绝对路径
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
struts.xml
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
</head>
<%
String path = request.getContextPath();
System.out.println(path);
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println(basePath);
%>
<body>
上传成功!<br>
文件标题:<s:property value="title"/><br>
文件为:<img src="<%=basePath%><s:property value="'uploadFiles/'+uploadfileFileName"/>"/><br>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.jsp' starting page</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="/Upload/hello/login" method="post" enctype="multipart/form-data">
文件名:<input type="text" name="title"/>
文件:<input type="file" name="uploadfile"/>
<input type="submit"/>
</form>
</body>
</html>
![](https://img-blog.csdn.net/20140301212342328?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFudGluZ21laQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
UploadServlet.java
package action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
//注释3.0版本使用
@MultipartConfig
public class UploadServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Part part=request.getPart("file");
response.setContentType("text/html;charset=GBK");
PrintWriter out=response.getWriter();
String fileName=request.getParameter("name");
out.println("上传文件的类型:"+part.getContentType()+"<br/>");
out.println("上传文件大小:"+part.getSize()+"<br/>");
Collection<String> headerNames=part.getHeaderNames();
for(String headerName:headerNames){
out.println(headerName+"---->"+part.getHeader(headerName)+"<br/>");
}
part.write(getServletContext().getRealPath("/uploadFiles")+"/"+fileName);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>upload</servlet-name>
<servlet-class>action.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>upload</servlet-name>
<url-pattern>/uploadServlet</url-pattern>
</servlet-mapping>
</web-app>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.jsp' starting page</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="/Uploads/uploadServlet" method="post" enctype="multipart/form-data">
文件名:<input type="text" name="name"/>
文件:<input type="file" name="file"/>
<input type="submit"/>
</form>
</body>
</html>
第二种采用struts2文件上传:
UploadAction.java
package action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String title;
private File uploadfile;
private String uploadfileContentType;
private String uploadfileFileName;
private String savePath;
public String upload() throws Exception {
FileInputStream fis = new FileInputStream(getUploadfile());
FileOutputStream fos=new FileOutputStream(getSavePath()+"\\"+getUploadfileFileName());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
fos.close();
fis.close();
return SUCCESS;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUploadfile() {
return uploadfile;
}
public void setUploadfile(File uploadfile) {
this.uploadfile = uploadfile;
}
public String getUploadfileContentType() {
return uploadfileContentType;
}
public void setUploadfileContentType(String uploadfileContentType) {
this.uploadfileContentType = uploadfileContentType;
}
public String getUploadfileFileName() {
return uploadfileFileName;
}
public void setUploadfileFileName(String uploadfileFileName) {
this.uploadfileFileName = uploadfileFileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath(savePath);//得到绝对路径
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
struts.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> <package name="hello" namespace="/hello" extends="struts-default"> <action name="login" class="action.UploadAction" method="upload"> <param name="savePath">/uploadFiles</param> <result name="success"> /success.jsp </result> </action> </package> </struts>
success.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
</head>
<%
String path = request.getContextPath();
System.out.println(path);
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println(basePath);
%>
<body>
上传成功!<br>
文件标题:<s:property value="title"/><br>
文件为:<img src="<%=basePath%><s:property value="'uploadFiles/'+uploadfileFileName"/>"/><br>
</body>
</html>
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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>My JSP 'index.jsp' starting page</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="/Upload/hello/login" method="post" enctype="multipart/form-data">
文件名:<input type="text" name="title"/>
文件:<input type="file" name="uploadfile"/>
<input type="submit"/>
</form>
</body>
</html>
相关文章推荐
- 用java代码实现从excel表格读取数据然后写入生成国际化配置文件properties
- myeclipse添加server library
- MyEclipse项目里面出现红叉,项目内又没有红叉的解决方案
- JAVA中implement和extends的区别
- java 通过SSH方式连接远程主机并上传和下载文件
- java基础学习记录之利用数组查表法进行进制转换优化的学习与练习七(十转二、十转八、十转十六)
- java基础学习记录之利用数组查表法进行进制转换的学习与练习六
- 详解struts2中struts.properties
- Eclipse上GIT插件EGIT使用手册
- java基础学习记录之利用数组进行进制转换的学习与练习五
- java命名规则和数据类型
- 基于struts2的校验示例
- java eclipse安装和android环境安装
- 一个关于Java程序安全意识重构的工具
- struts2 资源文件
- 关于JAVA多线程Runnable和Thread
- Java Web开发环境配置
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
- java精华3
- eclipse插件安装说明及使用方式