前面受一些非计算机专业的朋友所邀,想做一个文件上传的例子,在这里就写一个简单的例子实现jsp+servlet来完成文件的上传。废话不多说了,下面来看看代码:
1、首先,我们需要下载commons-fileupload.jar
和commons-io.jar两个包。有了这两个包我们就可以开始写代码了。
首先来看看jsp代码吧,在index.jsp文件中写入
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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>My JSP 'index.jsp' starting page</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="fileUpload" method="POST" enctype="Multipart/form-data">
<div align="right">
<input accept="*.jsp" id="myFile" name="myFrame1" type="file"/><br>
<input accept="*.jsp" id="myFile" name="myFrame2" type="file"/><br>
<input accept="*.jsp" id="myFile" name="myFrame3" type="file"/><br>
<input accept="*.jsp" id="myFile" name="myFrame4" type="file"/><br>
<input accept="*.jsp" id="myFile" name="myFrame5" type="file"/><br>
<input type="submit" value="上传"/>
</div>
</form>
</body>
</html>
然后在我们的myeclipse中,新建一个servlet,我这里命名FileUpload.java并放到了我自己创建的一个包com.ynshun.servlet中(根据自己的需要命名)。然后在FileUpload.java文件中输入如下代码:
package com.ynshun.servlet;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*
* @author 余能顺
* 2011-4-8 下午10:24:09
* FileUpload.java
* version 1.0
*/
public class FileUpload extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
PrintWriter out = response.getWriter();
DiskFileItemFactory fac = new DiskFileItemFactory();
fac.setSizeThreshold(1024 * 1024);
fac.setRepository(fac.getRepository());
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setFileSizeMax(1024 * 1024 * 5);
List fileList = null;
try {
fileList = upload.parseRequest(request);//获取上传列表
} catch (FileUploadException ex) {
ex.printStackTrace();
return;
}
Iterator iter = fileList.iterator();
while (iter.hasNext()) {
FileItem fileItem = (FileItem) iter.next();
if (!fileItem.isFormField()) {
String fileSize = new Long(fileItem.getSize()).toString();
String serverDir = getServletContext().getRealPath("uploadFiles");//设置文件上传目录
String filePath = serverDir + "\\" + fileItem.getName();
File saveFile = new File(filePath);
try {
fileItem.write(saveFile);
} catch (Exception ex1) {
ex1.printStackTrace();
return;
}
}
}
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);// 调用doGet方法,就不用写两个接受方式的代码了
}
}
好了,写到这里,已经完成了通过jsp页面选择上传文件,然后上传到指定的服务器地址里面了。我把源码也放到了这里,有需要学习的朋友可以下下去看一下。