求使用FileUpload来上传文件的可用代码

Rance 2010-03-19 01:43:36
要上传的文件名是从一个页面传过来的file_name值
该表单代码如下:
<FORM METHOD="POST" ACTION="upload_result.jsp" ENCTYPE="multipart/form-data">
<INPUT TYPE="FILE" name="file_name" SIZE="50"><BR>
<INPUT TYPE="SUBMIT" VALUE="Upload">
</FORM>

如何将该文件保存到我本地的某个路径下呢?
请大家提供可用的JSP或者Servlet代码,非常感谢!

注意,我没有用STRUTS等框架,希望能用COMMONS FILEUPLOAD解决上传问题
...全文
145 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
licip 2010-03-21
  • 打赏
  • 举报
回复
http://download.csdn.net/source/1089134
这个去看看呀。
  • 打赏
  • 举报
回复
首先把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

Rance 2010-03-21
  • 打赏
  • 举报
回复
没人用过fileupload么???????
Rance 2010-03-20
  • 打赏
  • 举报
回复
1,2楼没看题目么?
你们给出的都是使用SMARTUPLOAD的代码,我要用到是APACHE COMMONS FILEUPLOAD...
ladybirds2008 2010-03-19
  • 打赏
  • 举报
回复
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啦
vitahuang 2010-03-19
  • 打赏
  • 举报
回复

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());
}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧