Struts里文件的上传和下载 从来没有写过这方面的例子,不知道从哪里入手。谁有这方面的例子发给我一份学习,多谢!! Email: [email protected] 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 网上视频都有下载的。不要太懒了。struts有12之分的。 我有用struts2写的图片上传的(因为没有做文件类型的拦截,是可以上传任何文件的)例子:UpdloadAction.javapackage liu_upload;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.Statement;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import sun.misc.Request;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport{ private String title; private File upload; private String uploadContentType; private String uploadFileName; private String savePath; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public File getUpload() { return upload; } public void setUpload(File upload) { this.upload = upload; } public String getUploadContentType() { return uploadContentType; } public void setUploadContentType(String uploadContentType) { this.uploadContentType = uploadContentType; } public String getUploadFileName() { return uploadFileName; } public void setUploadFileName(String uploadFileName) { this.uploadFileName = uploadFileName; } public String getSavePath()throws Exception { return ServletActionContext.getRequest().getRealPath(savePath);//过时的方法 } public void setSavePath(String value) { this.savePath = value; } public String execute() throws Exception { // System.out.println(getTitle());// String PicPath=getSavePath()+"\\"+getUploadFileName();// String Driver = "com.mysql.jdbc.Driver";// String URL = "jdbc:mysql://localhost:3306/liu";// String user = "root";// String pass = "123";// Class.forName(Driver);// Connection conn = DriverManager.getConnection(URL, user, pass);// 这里做的是保存图片的路径// System.out.println(PicPath);// String rePicPath=PicPath.replace("\\", "/");// String sql = "insert into img values('"+rePicPath+"')";// String path="upload"+"\\"+getUploadFileName();// String repath=path.replace("\\", "/");// String sql = "insert into img values('"+repath+"')";// ResultSet rs = null;// Statement st = null;// st = conn.createStatement();// int n=st.executeUpdate(sql);// if(n==1){// System.out.println("-----------------------success!-----------------------");// }else{// System.out.println("-----------------------falt!-----------------------");// } String PicPath=getSavePath()+"\\"+getUploadFileName(); FileOutputStream fos=new FileOutputStream(PicPath); FileInputStream fis=new FileInputStream(getUpload()); // System.out.println("------------"+getSavePath()+"--------------"); byte[]buffer=new byte[1024]; int len=0; while((len=fis.read(buffer))>0){ fos.write(buffer,0,len); } return SUCCESS; } }struts.xml<?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><package name="liu_upload" extends="struts-default"> <action name="upload" class="liu_upload.UploadAction"> <param name="savePath">/upload</param> <result>/succ.jsp</result> </action> </package> </struts> web.xml<?xml version="1.0" encoding="UTF-8"?><web-app 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"> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <!-- struts2的核心技术 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>succ.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="java.sql.*"%><%@ taglib prefix="s" uri="/struts-tags"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>成功页面</title> </head> <body> 上传成功<br/> 文件标题:<s:property value="+title"/><br/> 文件为:<img src="<s:property value="'upload/'+uploadFileName"/>"/><br/> </body></html>Upload.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <title>文件上传</title> </head> <body> <form action="upload.action" method="post" enctype="multipart/form-data"> 标题:<input type="text" name="title"><br/> 选择文件:<input type="file" name="upload"><br/> <input value="上传" type="submit"> </form> </body></html> 这么巧,我也在写这个,我这边找到的资料都发给你吧,还有我自己改的jar包,有问题可以一起讨论。 最好是完整的项目,能直接运行出来的,这样直观感受挺好的。最好是Struts1的。 不知道你用的是strust1还是strust2.这里是struts2的上传下载http://blog.csdn.net/myeclipse_java/archive/2008/03/03/2143986.aspx 下载http://www.blogjava.net/max/archive/2007/03/21/105124.html 上传 呵呵,大家好!我是猎头cindy,我们公司是一家专门focus在IT行业的猎头公司,目前有高级JAVA开发工程师,软件架构师和系统分析工程师的职位,感兴趣的朋友可以加我的msn:[email protected],我们详细沟通,呵呵,不考虑也可以加我的,我们保持联系:-) 关于velocity数组访问的问题 下面这段浮动并每隔一行停顿一次的JS程序,无法在末尾的时候无缝自动接续。而是跳一下接续,求高手帮忙看一看? 【提问】javabean连接Mysql数据库问题 javascript路径问题? 请教--关于Struts防止重复提交问题 请教高手 jsp中运用css实现简单布局时遇到问题 传值的问题 购物车系统怎么实现阿? 求教 jsp对象传输问题 做网页时,从“本地上传图片”那个功能怎么实现
UpdloadAction.javapackage liu_upload;import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;import sun.misc.Request;import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class UploadAction extends ActionSupport{
private String title;
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath()throws Exception {
return ServletActionContext.getRequest().getRealPath(savePath);//过时的方法
}
public void setSavePath(String value) {
this.savePath = value;
}
public String execute() throws Exception {
// System.out.println(getTitle());
// String PicPath=getSavePath()+"\\"+getUploadFileName();
// String Driver = "com.mysql.jdbc.Driver";
// String URL = "jdbc:mysql://localhost:3306/liu";
// String user = "root";
// String pass = "123";
// Class.forName(Driver);
// Connection conn = DriverManager.getConnection(URL, user, pass);
// 这里做的是保存图片的路径
// System.out.println(PicPath);
// String rePicPath=PicPath.replace("\\", "/");
// String sql = "insert into img values('"+rePicPath+"')";
// String path="upload"+"\\"+getUploadFileName();
// String repath=path.replace("\\", "/");
// String sql = "insert into img values('"+repath+"')";
// ResultSet rs = null;
// Statement st = null;
// st = conn.createStatement();
// int n=st.executeUpdate(sql);
// if(n==1){
// System.out.println("-----------------------success!-----------------------");
// }else{
// System.out.println("-----------------------falt!-----------------------");
// }
String PicPath=getSavePath()+"\\"+getUploadFileName();
FileOutputStream fos=new FileOutputStream(PicPath);
FileInputStream fis=new FileInputStream(getUpload());
// System.out.println("------------"+getSavePath()+"--------------");
byte[]buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}
return SUCCESS;
}
}struts.xml<?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>
<package name="liu_upload" extends="struts-default">
<action name="upload" class="liu_upload.UploadAction">
<param name="savePath">/upload</param>
<result>/succ.jsp</result>
</action>
</package>
</struts>
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
<!-- struts2的核心技术 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
succ.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8" import="java.sql.*"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>成功页面</title>
</head>
<body>
上传成功<br/>
文件标题:<s:property value="+title"/><br/>
文件为:<img src="<s:property value="'upload/'+uploadFileName"/>"/><br/>
</body>
</html>
Upload.jsp<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>文件上传</title>
</head>
<body>
<form action="upload.action" method="post" enctype="multipart/form-data">
标题:<input type="text" name="title"><br/>
选择文件:<input type="file" name="upload"><br/>
<input value="上传" type="submit">
</form>
</body>
</html>
这里是struts2的上传下载
http://blog.csdn.net/myeclipse_java/archive/2008/03/03/2143986.aspx 下载
http://www.blogjava.net/max/archive/2007/03/21/105124.html 上传