---
title: "利用jspSmartUpload组件进行文件上传"
author: "jingrenlang"
date: "2009-03-27T20:10:06Z"
tags: ["F#", "HTML", "JSP", "Web"]
slug: "jspsmartupload"
draft: false
lastmod: "2009-03-27T20:12:35Z"
id: 356351
url: "https://jingrenlang.iteye.com/blog/356351"
---

今天，从网上查找下了java中有关文件上传的知识，从中了解到jspSmartUpload组件做文件上传与下载（支持多文件上传与下载）很简单，也很方便。于是，参考了网上与书本上的某些例子，试着做个了小例子。在这和大家一起分享。

      首先，我们需要下载jspSmartUpload组件jar包（在这里各位可以从附件中下载），接着，建立web项目，将jspSmartUpload组件jar包导入到web项目的WEB-INF/lib下。然后，编写相关代码。

```
文件上传Upload.jsp
```

```
<html>
<head>
<title>文件上传</title>
</head>
<body>
<!-- 在这 method要通过post方式提交，
     同时需在form中加入enctype="multipart/form-data",表示文件以流的方式上传。-->
<form action="FileUpload.jsp" method="post" enctype="multipart/form-data">
	<input type="file" name="file1"/><br>
	<input type="file" name="file2"/><br>
	<input type="file" name="file3"/><br>
	<input type="file" name="file4"/><br>
	<input type="submit" value="上传"/>
</form>
</body>
</html>
```

 

```
处理文件上传FileUpload.jsp
```

```
<html>
<head>
<title>文件上传处理</title>
</head>
<body>
<jsp:useBean id="upload" scope="page" class="com.jspsmart.upload.SmartUpload"></jsp:useBean>
<%
	upload.initialize(pageContext);
	try{
	     upload.upload();
	      for(int i=0;i<upload.getFiles().getCount();i++){
		com.jspsmart.upload.File file=upload.getFiles().getFile(i);
		out.println("<br>文件长度："+file.getSize());
		out.println("<br>文件名："+file.getFieldName());
		//save的第一参数表示文件上传哪，即文件是上传目录；第二个参数表示目录的类型
	          //目录类型有三种，具体是那三种，查看附件中的帮助文档
	          //在这我采用的是以操作系统的根目录为文件根目录另存文件
		upload.save("F:/sxt",upload.SAVE_PHYSICAL);
	     }
	}catch(Exception e){
		out.println("上传失败！！！");
		return;
	}
	out.println("上传成功！！！");
 %>

</body>
</html>
```