在项目中上传下载是必不可少的,在这里我将介绍下上传下载,当然代码写的不太好
但是还是可以给新手参考,学习下子咧
代码如下:
前台:<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="cn.laofei.vo.Utility"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%
Utility utility = new Utility(); //取ID 目的是为了让上传的文件名不重复 当然这个ID你也可以自己取,不过我还是建议用这个方法取(年月日分秒,这样文件名就不会重复) 此类项目中 下载即可
String newid = utility.getId();
%>
<html>
<head>
<title>上传,下传,www.liuzm.com</title>
</head>
<body bgcolor="#FFFFFF" text="#000000"><p><font size="5"color="#FF0000">
支持多上传
<form action="Upload?id=<%=newid%>" method="post" enctype="multipart/form-data" name="form1">
<!-- 记住 form 表单中一定不要忘记加了 enctype="multipart/form-data" -->
<input type="file" name="file"></br>
<input type="file" name="file1">
<input type="hidden" name="id" value="<%=newid%>"></br>
<input type="submit" name="Submit" value="upload">
</form>
servlet类:
package com.liuzm;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
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.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
public class Upload extends HttpServlet {
File tmpDir =null;//初始化上传文件的临时存放目录
File saveDir =null;//初始化上传文件后的保存目录
public Upload() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String id = request.getParameter("id");
// id+=".jpg";
try{
if(ServletFileUpload.isMultipartContent(request)){
DiskFileItemFactory dff = new DiskFileItemFactory();//创建该对象
dff.setRepository(tmpDir);//指定上传文件的临时目录
dff.setSizeThreshold(1024000);//指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);//创建该对象
sfu.setFileSizeMax(5000000);//指定单个上传文件的最大尺寸
sfu.setSizeMax(10000000);//指定一次上传多个文件的总尺寸
FileItemIterator fii = sfu.getItemIterator(request);//解析request 请求,并返回FileItemIterator集合
while(fii.hasNext()){
FileItemStream fis = fii.next();//从集合中获得一个文件流
if(!fis.isFormField() && fis.getName().length()>0){//过滤掉表单中非文件域
System.out.println(fis.getName().substring(fis.getName().lastIndexOf(".")));
String idd = fis.getName().substring(fis.getName().lastIndexOf("."));//取文件的扩展名
id+=idd;
System.out.println(fis.getName());
String fileName = fis.getName().substring(fis.getName().lastIndexOf("."));//获得上传文件的文件名
String liu =fileName.replace(fileName, id);
BufferedInputStream in = new BufferedInputStream(fis.openStream());//获得文件输入流
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+"\\"+liu)));//获得文件输出流
String patha = saveDir+"\\"+liu; //文件路径
System.out.println(patha+"\\");
Streams.copy(in, out, true);//开始把文件写到你指定的上传文件夹
}
}
response.getWriter().println("File upload successfully!!!");//终于成功了
}
}catch(Exception e){
e.printStackTrace();
}
}
public void init() throws ServletException {
super.init();
String tmpPath = "c:\\tmpdir\\";
String savePath = "c:\\liuupdir\\";
tmpDir = new File(tmpPath);
saveDir = new File(savePath);
if(!tmpDir.isDirectory())
tmpDir.mkdir();
if(!saveDir.isDirectory())
saveDir.mkdir();
}
}
本篇文章来源于 :刘志猛博客 原文链接:http://www.liuzm.com/article/java/9628.htm
http://www.liuzm.com