求使用FileUpload来上传文件的可用代码 本帖最后由 Rance 于 2010-03-19 01:45:49 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 String username="";SmartUpload mySmartUpload =new SmartUpload(); long file_size_max=5242880; //文件大小String fileName2="",ext="",testvar="";String url="selfimages/"; //应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹)//初始化mySmartUpload.initialize(pageContext);//只允许上载此类文件try{ //上载文件 mySmartUpload.upload(); if(mySmartUpload.getRequest().getParameter("username")==null) { out.print("<script>alert('操作有误,请重新操作!');history.go(-1);</script>"); return; } username=mySmartUpload.getRequest().getParameter("username"); } catch(Exception e) {} try { com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0); if (myFile.isMissing()){out.println("Missing");} else { ext= myFile.getFileExt(); //取得后缀名 int file_size=myFile.getSize(); //取得文件的大小 String saveurl=""; if(file_size<file_size_max) { saveurl=application.getRealPath("/")+url; saveurl+=username+".*** "; //上传文件的后缀名(***改成自己要的文件后缀) myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL); out.println("<script>document.location.href = '*****.jsp;'</script>"); } } } catch (Exception e) { out.print(e.toString()); } String username="";SmartUpload mySmartUpload =new SmartUpload(); long file_size_max=5242880; //文件大小String fileName2="",ext="",testvar="";String url="selfimages/"; //应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹)//初始化mySmartUpload.initialize(pageContext);//只允许上载此类文件try{//上载文件mySmartUpload.upload();if(mySmartUpload.getRequest().getParameter("username")==null){out.print("<script>alert('操作有误,请重新操作!');history.go(-1);</script>");return;}username=mySmartUpload.getRequest().getParameter("username");}catch(Exception e){}try{ com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);if (myFile.isMissing()){out.println("Missing");}else{ext= myFile.getFileExt(); //取得后缀名int file_size=myFile.getSize(); //取得文件的大小 String saveurl="";if(file_size<file_size_max){saveurl=application.getRealPath("/")+url;saveurl+=username+".*** "; //上传文件的后缀名(***改成自己要的文件后缀)myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL);out.println("<script>document.location.href = '*****.jsp;'</script>");}}}catch (Exception e){out.print(e.toString());}ok啦 1,2楼没看题目么?你们给出的都是使用SMARTUPLOAD的代码,我要用到是APACHE COMMONS FILEUPLOAD... 没人用过fileupload么??????? 首先把fileupload的jar包导入进去,步骤:1.创建一个Servlet----Upload.java文件,用于实现上传文件import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;import org.apache.commons.fileupload.*;public class Upload extends HttpServlet { private String uploadPath = "d:\\upload\\"; // 上传文件的目录 private String tempPath = "d:\\upload\\tmp\\"; // 临时文件目录 在doPost()方法中,当servlet收到浏览器发出的Post请求后,实现文件上传。以下是示例代码:public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{ try { DiskFileUpload fu = new DiskFileUpload(); // 设置最大文件尺寸,这里是4MB fu.setSizeMax(4194304); // 设置缓冲区大小,这里是4kb fu.setSizeThreshold(4096); // 设置临时目录: fu.setRepositoryPath(tempPath); // 得到所有的文件: List fileItems = fu.parseRequest(request);//解析用户请求的参数,取出文件上传信息 Iterator i = fileItems.iterator(); // 依次处理每一个文件: while(i.hasNext()) { FileItem fi = (FileItem)i.next(); // 获得文件名,这个文件名包括路径: String fileName = fi.getName(); // 在这里可以记录用户和文件信息 // ... // 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名: fi.write(new File(uploadPath + "a.txt")); } } catch(Exception e) { // 可以跳转出错页面 }}编译该servlet,注意要指定classpath,确保包含commons-upload-1.0.jar和tomcat\common\lib\servlet-api.jar。2.配置servlet,用记事本打开tomcat\webapps\你的webapp\WEB-INF\web.xml,没有的话新建一个。典型配置如下:<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app> <servlet> <servlet-name>Upload</servlet-name> <servlet-class>Upload</servlet-class> </servlet> <servlet-mapping> <servlet-name>Upload</servlet-name> <url-pattern>/fileupload</url-pattern> </servlet-mapping></web-app>3.配置好servlet后,启动tomcat,写一个简单的html测试:<form action="fileupload" method="post"enctype="multipart/form-data" name="form1"> <input type="file" name="file"> <input type="submit" name="Submit" value="upload"></form>注意action="fileupload"其中fileupload是配置servlet时指定的url-pattern http://download.csdn.net/source/1089134这个去看看呀。 有Struts 2.0经验的请进! jdbc更新多条记录问题 dom4j操作问题 如何将一个变量值放入cookie?(回答就有分) 求高手求出下面的sql语句 初学jstl,请教各位大侠 问一个javabean连接数据库的问题!! JSP中文输入输出问题 初学的··请大虾们看看那有错误!!!谢谢了~~~ 大神们,帮看下这个问题 请大家帮帮忙啊?: Error creating bean with name 'orgManager' defined in file Error crea 想用自己的电脑安装svn应该如何操作
String username="";
SmartUpload mySmartUpload =new SmartUpload();
long file_size_max=5242880; //文件大小
String fileName2="",ext="",testvar="";
String url="selfimages/"; //应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹)
//初始化
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try
{
//上载文件
mySmartUpload.upload();
if(mySmartUpload.getRequest().getParameter("username")==null)
{
out.print("<script>alert('操作有误,请重新操作!');history.go(-1);</script>");
return;
}
username=mySmartUpload.getRequest().getParameter("username");
}
catch(Exception e)
{}
try
{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (myFile.isMissing()){out.println("Missing");}
else
{
ext= myFile.getFileExt(); //取得后缀名
int file_size=myFile.getSize(); //取得文件的大小
String saveurl="";
if(file_size<file_size_max)
{
saveurl=application.getRealPath("/")+url;
saveurl+=username+".*** "; //上传文件的后缀名(***改成自己要的文件后缀)
myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL);
out.println("<script>document.location.href = '*****.jsp;'</script>");
}
}
}
catch (Exception e)
{
out.print(e.toString());
}
SmartUpload mySmartUpload =new SmartUpload();
long file_size_max=5242880; //文件大小
String fileName2="",ext="",testvar="";
String url="selfimages/"; //应保证在根目录中有此目录的存在(也就是说需要自己建立相应的文件夹)
//初始化
mySmartUpload.initialize(pageContext);
//只允许上载此类文件
try
{
//上载文件
mySmartUpload.upload();if(mySmartUpload.getRequest().getParameter("username")==null)
{
out.print("<script>alert('操作有误,请重新操作!');history.go(-1);</script>");
return;
}
username=mySmartUpload.getRequest().getParameter("username");
}
catch(Exception e)
{}
try
{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(0);
if (myFile.isMissing()){out.println("Missing");}
else
{
ext= myFile.getFileExt(); //取得后缀名
int file_size=myFile.getSize(); //取得文件的大小
String saveurl="";
if(file_size<file_size_max)
{
saveurl=application.getRealPath("/")+url;
saveurl+=username+".*** "; //上传文件的后缀名(***改成自己要的文件后缀)
myFile.saveAs(saveurl,SmartUpload.SAVE_PHYSICAL);
out.println("<script>document.location.href = '*****.jsp;'</script>");
}
}
}
catch (Exception e)
{
out.print(e.toString());
}
ok啦
你们给出的都是使用SMARTUPLOAD的代码,我要用到是APACHE COMMONS FILEUPLOAD...
步骤:1.创建一个Servlet----Upload.java文件,用于实现上传文件import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.fileupload.*;
public class Upload extends HttpServlet {
private String uploadPath = "d:\\upload\\"; // 上传文件的目录
private String tempPath = "d:\\upload\\tmp\\"; // 临时文件目录 在doPost()方法中,当servlet收到浏览器发出的Post请求后,实现文件上传。以下是示例代码:
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
try {
DiskFileUpload fu = new DiskFileUpload();
// 设置最大文件尺寸,这里是4MB
fu.setSizeMax(4194304);
// 设置缓冲区大小,这里是4kb
fu.setSizeThreshold(4096);
// 设置临时目录:
fu.setRepositoryPath(tempPath); // 得到所有的文件:
List fileItems = fu.parseRequest(request);//解析用户请求的参数,取出文件上传信息
Iterator i = fileItems.iterator();
// 依次处理每一个文件:
while(i.hasNext()) {
FileItem fi = (FileItem)i.next();
// 获得文件名,这个文件名包括路径:
String fileName = fi.getName();
// 在这里可以记录用户和文件信息
// ...
// 写入文件,暂定文件名为a.txt,可以从fileName中提取文件名:
fi.write(new File(uploadPath + "a.txt"));
}
}
catch(Exception e) {
// 可以跳转出错页面
}
}编译该servlet,注意要指定classpath,确保包含commons-upload-1.0.jar和tomcat\common\lib\servlet-api.jar。
2.配置servlet,用记事本打开tomcat\webapps\你的webapp\WEB-INF\web.xml,没有的话新建一个。
典型配置如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>
<servlet>
<servlet-name>Upload</servlet-name>
<servlet-class>Upload</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>Upload</servlet-name>
<url-pattern>/fileupload</url-pattern>
</servlet-mapping>
</web-app>3.配置好servlet后,启动tomcat,写一个简单的html测试:
<form action="fileupload" method="post"
enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="submit" name="Submit" value="upload">
</form>
注意action="fileupload"其中fileupload是配置servlet时指定的url-pattern
这个去看看呀。