admin 管理员组

文章数量: 887021

common

1.要实现common-fileupload实现文件上传功能,要在项目下面导入common-fileupload和common-io两个jar包。

2.upload.jsp

代码如下:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
<script src=".3.1/jquery.min.js"></script>
</head>
<script type="text/javascript">function uploadmethod(){var formData=new FormData();formData.append('file',$('#file')[0].files[0]);$.ajax({url: 'uploadServlet.do',type: 'POST',cache: false,data: formData,dataType:"json",processData: false,contentType: false}).done(function(res) {console.log(res);}).fail(function(res) {});}
</script>
<body>
<form id="uploadForm" enctype="multipart/form-data"><input id="file" type="file" name="file"/><img alt="" src=""><button onclick="uploadmethod()" id="upload" type="button">upload</button>
</form>
</body>
</html>

3.UploadServlet.java

代码如下:

package com.dfrz.servlet;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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.apachemons.fileupload.FileItem;
import org.apachemons.fileupload.FileItemFactory;
import org.apachemons.fileupload.FileUploadException;
import org.apachemons.fileupload.disk.DiskFileItemFactory;
import org.apachemons.fileupload.servlet.ServletFileUpload;public class UploadServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();// 使用common-filepload进行处理上传内容FileItemFactory factory = new DiskFileItemFactory();ServletFileUpload fileUpload = new ServletFileUpload(factory);// factory工厂 , FileItem是单个文件System.out.println(111);try {//parseRequest(request)需要导入带有common的包,不然会报错List<FileItem> files = fileUpload.parseRequest(request);for (FileItem fileItem : files) {// 文件名System.out.println(fileItem.getName());System.out.println(fileItem.getSize());System.out.println(fileItem.getContentType());// 进行文件写到服务器磁盘// 获取服务器路径String parentDir = this.getServletContext().getRealPath("/upload");// 如果服务器没有upload路径,要创建文件夹File file = new File(parentDir, fileItem.getName());if (!file.getParentFile().exists()) {file.getParentFile().mkdir();}// 进行文件写到服务器磁盘 IO读写InputStream is = fileItem.getInputStream();OutputStream os = new FileOutputStream(file);byte[] buf = new byte[1024];int len = -1;while ((len = is.read(buf)) != -1) {os.write(buf, 0, len);}// 关闭流os.close();is.close();out.println("{\"status\":\"1\",\"fpath\":\"" + fileItem.getName() + "\"}");}} catch (FileUploadException e) {// TODO Auto-generated catch blocke.printStackTrace();}}
}

4.其中,文件保存在自己设置的根目录下。实在找不到的话,可以在String parentDir = this.getServletContext().getRealPath("/upload");处设置一个断点,然后查看文件所保存在哪里。

本文标签: common