当前位置: 代码迷 >> Web前端 >> FileUpLoad实现文件下传与上载
  详细解决方案

FileUpLoad实现文件下传与上载

热度:605   发布时间:2012-09-13 09:51:53.0
FileUpLoad实现文件上传与下载

首先需要两个第三方Jar包

上传与下载文件的Servlet

package com.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.dao.UpFileDAO;
import com.vo.UpFileVo;

public class UpLoadServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");

		// 从硬盘下载文件
		// String fileName=request.getParameter("fileName");
		//		
		// String path = super.getServletContext().getRealPath("\\");
		//
		// File file = new File(path + "upload\\" + fileName);
		//
		// if(file.exists()){

		// 从数据库下载文件
		int id = Integer.parseInt(request.getParameter("id"));

		UpFileDAO upDao = new UpFileDAO();

		UpFileVo upfile = upDao.findFileById(id);// 得到文件

		// InputStream input=null;
		OutputStream output = null;

		try {
			// input=new FileInputStream(file);
			//				
			// byte[] size=new byte[input.available()];
			//				
			// input.read(size);

			// 设置下载的mime类型
			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition", "attachment; filename="
					+ upfile.getFileName());

			output = response.getOutputStream();

			output.write(upfile.getFilecontent());

			output.flush();

		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			if (output != null) {
				output.close();
			}
			// if(input!=null){
			// input.close();
			// }
		}
	}

	// }

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 判断是否有属性mutilpart
		boolean isMultipart = ServletFileUpload.isMultipartContent(request);

		try {
			if (isMultipart) {
				// 创建文件列表工厂
				FileItemFactory factory = new DiskFileItemFactory();

				ServletFileUpload upload = new ServletFileUpload(factory);

				List<FileItem> items = upload.parseRequest(request);

				UpFileDAO upDao = new UpFileDAO();

				// 找到文件域
				for (FileItem fileItem : items) {
					if (!fileItem.isFormField()) {
						// 获得文件的一些信息
						// System.out.println(fileItem.getFieldName());
						// System.out.println(fileItem.getName());
						// System.out.println(fileItem.getSize());
						// System.out.println(fileItem.get()); // 文件内容

						// 获得上传的文件名字
						String fileName = fileItem.getName();
						fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);// 文件的名字

						// 获取上传文件的路径
						String path = super.getServletContext().getRealPath("\\");

						File file = new File(path + "upload\\" + fileName);

						OutputStream output = null;
						//
						// try {
						// output = new FileOutputStream(file);
						//
						// output.write(fileItem.get());
						//
						// output.flush();
						// output.flush();
						// } catch (RuntimeException e) {
						// e.printStackTrace();
						// } finally {
						// if (output != null) {
						// output.close();
						// }
						// }

						// 将文件写到数据库中

						UpFileVo upfile = new UpFileVo();

						upfile.setFileName(fileName);
						upfile.setFileSize(fileItem.getSize());
						upfile.setFilecontent(fileItem.get());

						boolean b = upDao.insertUpFile(upfile);

						// 如果数据库添加失败,则将上传得文件删除
						if (!b) {
							fileItem.delete();
						}

					} else {
						if ("expireTime".equals(fileItem.getFieldName())) {
							System.out.println(fileItem.getString());
						}
					}
				}
			}
		} catch (FileUploadException e) {
			e.printStackTrace();
		}

		response.sendRedirect("UpFileListServlet");

	}
}

?

  相关解决方案