Uploadfil(文件下载)

1.com.xiao.filedown.FileDownLoad

package com.filedown;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.ServletUtils;

public class FileDownLoad extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//文件下载
		//1.下载的文件:请求参数filename获取
		String filename=request.getParameter("filename");
		//2.对url进行解码(主要目的解决url中的中文问题)
		filename=URLDecoder.decode(filename,"utf-8");
		System.out.println(filename);
		
		//3.文件的路径:绝对路径
		String downLoadDir="files";
		String absPathLoad=request.getSession().getServletContext()
					.getRealPath(downLoadDir+File.separator+filename);
		//4.设置响应类型:application/octet-stream;
		response.setContentType("application/octet-stream;");
		//5.设置响应头信息:指定下载的文件名称
		//下载的文件名称即下载后文件的名称
		byte[]bytes=filename.getBytes();
		String loadFile=new String(bytes,"iso-8859-1");
		response.setHeader("Content-Dispsition", "attachnement;filename="+loadFile);
		//6.获取Servlet字节输出流
		ServletOutputStream out=
				response.getOutputStream();
		//7.文件下载(把指定文件输出到客户端)--ServletUtils类在cos.jar文件
		ServletUtils.returnFile(absPathLoad, out);
	}

}




2.com.xiao.uploadfile.RegiterServelet

package com.uploadfile;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;

public class RegiterServelet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String saveDirName="upLoad"; //保存上传文件的目录名称
		//获取saveDirName目录在tomcat中的绝对路径
		String saveDirAbsPath=request.getSession().getServletContext()
				.getRealPath(saveDirName);
		//创建目录对象
		File dir=new File(saveDirAbsPath);
		//判断目录对象是否存在,不存在就创建
		if(!dir.exists()){
			dir.mkdir();			
		}
		//文件上传
		MultipartRequest multipart=
				new MultipartRequest(request,dir.getAbsolutePath()
						,5*1024*1024,"utf-8");
		//获取文件上传文件相对工程的目录:upLoad/文件名称
		// <input type="file" name="file1"/>:参数是file1
		// 获取<input type="file" name="file1"/>选择的上传文件对象
		File file=multipart.getFile("filel");
		String fileName=file.getName();
		System.out.println(fileName);
		
		String dbFilePath=saveDirName+"/"+fileName;
		System.out.println(dbFilePath);
		
		//获取提交的数据:姓名,电话
		String name=multipart.getParameter("uname");
		String mobile=multipart.getParameter("mobile");
		System.out.println(name+","+mobile);
	}

}




com.xiao.uploadfile.UpLoadFile

package com.uploadfile;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.*;
/*
 * 文件上传Servlet代码
 */
public class UpLoadFile extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
		
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//文件上传
		MultipartRequest multipart=
				new MultipartRequest(request,"F:\\mmm",
						5*1024*1024,"utf-8");
	}

}

com.xiao.uploadfile.UpLoadFile02

package com.uploadfile;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;

public class UpLoadFile02 extends HttpServlet {

	/*
	 * 文件上传servlet代码
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String saveDirName="upLoad"; //保存上传文件的路径
		
		//获取saveDirNmae目录在tomcat中的绝对路径
		String saveDirAbsPath=
			request.getSession().getServletContext()
			.getRealPath(saveDirName);
		//创建目录对象
		File dir=new File(saveDirAbsPath);
		//判断目录对象是否存在,不存在就创建
		if(!dir.exists()){
			dir.mkdir();			
		}
		//文件上传
		MultipartRequest multipart=
				new MultipartRequest(request, 
				dir.getAbsolutePath(),10*1024*1024,"utf-8");
		//获取上传文件相对工程的目录:upLoad/文件名称
		//<input type="file" name="file1"/>:参数是file1
		//获取<input type="file" name="file1"/>选择的文件对象
		File file=multipart.getFile("file1");
		String fileName=file.getName();
		System.out.println(fileName);
		String dbFilePath=saveDirName+"/"+fileName;
		System.out.println(dbFilePath);
		
	}

}

com.xiao.uploadfile.UpLoadFile03

package com.uploadfile;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpLoadFile03 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			this.doPost(request, response);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String saveDirName="upLoad";//保存上传文件的目录名称
		//获取saveDirName目录在tomcat中的绝对路径
		String saveDirAbsPath=request.getSession().getServletContext().getRealPath(saveDirName);
		//创建目录对象
		File dir=new File(saveDirAbsPath);
		//判断目录文件是否存在,不存在就创建
		if(!dir.exists()){
			
		}
	}
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值