jsp实现文件的上传下载 求用struts+hibernate实现将文件上传至服务器及下载的完整代码。谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我只用过smartUpload组件。当然自己写也是可以的 http://www.blueidea.com/tech/program/2008/5258.asp struts无论哪个版本对上传是封装的,下载部分你要想简单好用可以考虑用我的这个http://topic.csdn.net/u/20100617/21/a6dccfb2-b793-4f24-a239-9acd65f1e2d7.html 不用那么复杂, struts有个 标签好像是 <c:file..> 可以实现上传, 楼主可以摆渡下, 下载就更容易了 只要url+文件名就可以。 这个不是就是跟我那个基本一样的- -不过struts还是推荐用他自己的上传方案 方案比较的话。就算使用其他方案也不复杂吧。只不过使用struts自带方案在验证方面有优势(进入逻辑类前验证)。struts1.x与struts2.x标签不一样你给的是2.x的吧 不过你居然拿c做标签prefix。下载只要url+文件名就可以了吗?那是需要http服务或ftp服务支持的好吧,安全性很难控制的。 file是不是只是将文件的路径传到数据库,要怎样实现将整个文件传到服务器 <input type="file" name="fileup">是把客户端文件以流的形式发送给服务器端,服务器端可以拿到这个流,从而获取文件名称,保存到自己本地(服务器端),将保存地址存入数据库。 struts不是自带了上传的组件吗? 通过FormFile 很容易啊。 去google 我有现成的struts+hibernate的上传代码,你可以留下邮箱,我发给你,但你要自己调试,在我这里运行,在你那里不一定正常运行了 struts1还是struts2?struts2在文件上传方面实现比较简单,楼主可以去电驴上面下载 建议用struts上传下载 代码量少 很快很方便! ACTION 代码package com.upload;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.Date;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class upload extends ActionSupport{ /** * */ private static final long serialVersionUID = 1L; private static final int BUFFER_SIZE = 16 * 1024 ; private File myFile; private String fileName; private String imageFileName; private String caption; public void setMyFileContentType(String contentType) { } public void setMyFileFileName(String fileName) { this .fileName = fileName; } public void setMyFile(File myFile) { this .myFile = myFile; } public String getImageFileName() { return imageFileName; } public String getCaption() { return caption; } public void setCaption(String caption) { this .caption = caption; } private static void copy(File src, File dst) { try { InputStream in = null ; OutputStream out = null ; try { in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE); out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE); byte [] buffer = new byte [BUFFER_SIZE]; while (in.read(buffer) > 0 ) { out.write(buffer); } } finally { if ( null != in) { in.close(); } if ( null != out) { out.close(); } } } catch (Exception e) { e.printStackTrace(); } } private static String getExtention(String fileName) { int pos = fileName.lastIndexOf("."); return fileName.substring(pos); } @Override public String execute() { imageFileName= new Date().getTime() + getExtention(fileName); File imageFile=new File(ServletActionContext.getServletContext().getRealPath("/UploadImages") +"/"+imageFileName); copy(myFile, imageFile); return SUCCESS; } }JSP代码<%@page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%><! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" > <html xmlns ="http://www.w3.org/1999/xhtml" > <head> <title> Struts 2 File Upload </title> </head> <body> <s:form action ="/upload.action" method ="POST" enctype ="multipart/form-data" > <s:file name="myFile" label ="Image File" /> <s:textfield name="caption" label="Caption" /> <s:submit /> </s:form > </body> </html> <package name ="fileUploadDemo" namespace="/" extends ="struts-default" > <action name="upload" class ="com.upload.upload" > <interceptor-ref name ="fileUpload" > <param name="allowedTypes"> image/bmp,image/png,image/jpg </param> </interceptor-ref> <interceptor-ref name="defaultStack" /> <result name="success">/ShowUpload.jsp</result > <result name="input">/Fileupload.jsp</result> </action > </package > Strust.xml JSP文件上传代码 实现~ 用JSP简单一文件很容易,可是上传的文件里前4行和后5行是表单域信息,中间部分才是客户提交的文件的内容.如何去掉表单域的信息,获取上传的内容?UploadFileExample.jsp<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %><%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="AcceptUploadFile.jsp" method="post" enctype="multipart/form-data"><br> 请选择要上传的文件:<input type="file" name="file" size=50> <input type="submit" value="提交"> </form> </body></html>AcceptUploadFile.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %><%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> <% String tempFileName=new String("tempFile1"); File tempFile1=new File("D:/Java/Tomcat 5.5/webapps/test5/",tempFileName); FileOutputStream outputFile=new FileOutputStream(tempFile1); InputStream fileSource=request.getInputStream(); byte b[]=new byte[1000]; int n; while((n=fileSource.read(b))!=-1) outputFile.write(b,0,n); outputFile.close(); fileSource.close(); RandomAccessFile randomFile1=new RandomAccessFile(tempFile1,"r"); randomFile1.readLine(); String FilePath=randomFile1.readLine(); int position=FilePath.lastIndexOf('\\'); String filename=FilePath.substring(position+1,FilePath.length()-1); filename=new String(filename.getBytes("iso-8859-1"),"GBK"); randomFile1.readLine(); randomFile1.readLine(); long forthEnterPosition= randomFile1.getFilePointer(); File FileUploadDir=new File("D:/Java/Tomcat 5.5/webapps/test5/","Upload"); FileUploadDir.mkdir(); File saveFile1=new File("D:/Java/Tomcat 5.5/webapps/test5/Upload/",filename); RandomAccessFile randomFile2=new RandomAccessFile(saveFile1,"rw"); long endPosition=randomFile1.length(); int j=1; while((endPosition>=0)&&(j<=4)) { endPosition--; randomFile1.seek(endPosition); if(randomFile1.readByte()=='\n') j++; } randomFile1.seek(forthEnterPosition); long startPoint=randomFile1.getFilePointer(); while(startPoint<endPosition-1) { randomFile2.write(randomFile1.readByte()); startPoint=randomFile1.getFilePointer(); } randomFile1.close(); randomFile2.close(); tempFile1.delete(); %> </body></html>参看:http://md20040208.blog.163.com/blog/static/30069427200881493933958/ 关于用lucene搜索返回文档内容的一个问题~~急求答案~~ log4j用DailyRollingFileAppender每半天切换一个文件,切换了,还是在写老日志文件 spring中泛型DAO的问题 救命,我要疯了 求哪位高手帮看下一个struts2与spring2.5整合的问题? SSH分层 控制未登录用户跳转到登陆界面 急在线等 请问怎么将Excel表格转换成XML文件? 在websphere下如何设置endorsed 郁闷啊 请教关于web国际化的问题!!! Java EE与数据库学习顺序与问题求助~~~~
http://topic.csdn.net/u/20100617/21/a6dccfb2-b793-4f24-a239-9acd65f1e2d7.html
这个不是就是跟我那个基本一样的- -
不过struts还是推荐用他自己的上传方案
struts1.x与struts2.x标签不一样你给的是2.x的吧 不过你居然拿c做标签prefix。
下载只要url+文件名就可以了吗?那是需要http服务或ftp服务支持的好吧,安全性很难控制的。
package com.upload;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class upload extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 16 * 1024 ;
private File myFile;
private String fileName;
private String imageFileName;
private String caption;
public void setMyFileContentType(String contentType) {
}
public void setMyFileFileName(String fileName) {
this .fileName = fileName;
}
public void setMyFile(File myFile) {
this .myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
} public void setCaption(String caption) {
this .caption = caption;
}
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
@Override
public String execute() {
imageFileName= new Date().getTime() + getExtention(fileName);
File imageFile=new File(ServletActionContext.getServletContext().getRealPath("/UploadImages") +"/"+imageFileName);
copy(myFile, imageFile);
return SUCCESS;
}
}JSP代码
<%@page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%><! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns ="http://www.w3.org/1999/xhtml" >
<head>
<title> Struts 2 File Upload </title>
</head>
<body> <s:form action ="/upload.action" method ="POST" enctype ="multipart/form-data" >
<s:file name="myFile" label ="Image File" />
<s:textfield name="caption" label="Caption" />
<s:submit />
</s:form >
</body>
</html>
<action name="upload" class ="com.upload.upload" >
<interceptor-ref name ="fileUpload" >
<param name="allowedTypes">
image/bmp,image/png,image/jpg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<result name="success">/ShowUpload.jsp</result >
<result name="input">/Fileupload.jsp</result>
</action >
</package >
<%
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="AcceptUploadFile.jsp" method="post" enctype="multipart/form-data"><br>
请选择要上传的文件:<input type="file" name="file" size=50>
<input type="submit" value="提交">
</form>
</body>
</html>AcceptUploadFile.jsp<%@ page contentType="text/html; charset=gb2312" language="java" import="java.io.*" errorPage="" %>
<%
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>
<%
String tempFileName=new String("tempFile1");
File tempFile1=new File("D:/Java/Tomcat 5.5/webapps/test5/",tempFileName);
FileOutputStream outputFile=new FileOutputStream(tempFile1);
InputStream fileSource=request.getInputStream();
byte b[]=new byte[1000];
int n;
while((n=fileSource.read(b))!=-1)
outputFile.write(b,0,n);
outputFile.close();
fileSource.close();
RandomAccessFile randomFile1=new RandomAccessFile(tempFile1,"r");
randomFile1.readLine();
String FilePath=randomFile1.readLine();
int position=FilePath.lastIndexOf('\\');
String filename=FilePath.substring(position+1,FilePath.length()-1);
filename=new String(filename.getBytes("iso-8859-1"),"GBK");
randomFile1.readLine();
randomFile1.readLine();
long forthEnterPosition= randomFile1.getFilePointer();
File FileUploadDir=new File("D:/Java/Tomcat 5.5/webapps/test5/","Upload");
FileUploadDir.mkdir();
File saveFile1=new File("D:/Java/Tomcat 5.5/webapps/test5/Upload/",filename);
RandomAccessFile randomFile2=new RandomAccessFile(saveFile1,"rw");
long endPosition=randomFile1.length();
int j=1;
while((endPosition>=0)&&(j<=4))
{
endPosition--;
randomFile1.seek(endPosition);
if(randomFile1.readByte()=='\n')
j++;
}
randomFile1.seek(forthEnterPosition);
long startPoint=randomFile1.getFilePointer();
while(startPoint<endPosition-1)
{
randomFile2.write(randomFile1.readByte());
startPoint=randomFile1.getFilePointer();
}
randomFile1.close();
randomFile2.close();
tempFile1.delete();
%>
</body>
</html>参看:http://md20040208.blog.163.com/blog/static/30069427200881493933958/