时间:2021-05-20
项目结构
src com servletdemo DownloadServlet.java ShowServlet.java UploadServlet.java WebContent jsp servlet download.html fileupload.jsp input.jsp WEB-INF lib commons-fileupload-1.3.1.jar commons-io-2.4.jar1.简单实例
ShowServlet.java
package com.servletdemo;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ShowServlet */@WebServlet("/ShowServlet")public class ShowServlet extends HttpServlet { private static final long serialVersionUID = 1L; PrintWriter pw=null; /** * @see HttpServlet#HttpServlet() */ public ShowServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub this.doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("gb2312"); response.setContentType("text/html;charset=gb2312"); pw=response.getWriter(); String name=request.getParameter("username"); String password=request.getParameter("password"); pw.println("user name:" + name); pw.println("<br>"); pw.println("user password:" + password); }}input.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://.servletdemo;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.ServletResponse; /** * Servlet implementation class DownloadServlet */@WebServlet("/DownloadServlet")public class DownloadServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DownloadServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub download1(response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } public void download1(HttpServletResponse response) throws IOException{ //获取所要下载文件的路径 String path = this.getServletContext().getRealPath("/files/web配置.xml"); String realPath = path.substring(path.lastIndexOf("\\")+1); //告诉浏览器是以下载的方法获取到资源 //告诉浏览器以此种编码来解析URLEncoder.encode(realPath, "utf-8")) response.setHeader("content-disposition","attachment; filename="+URLEncoder.encode(realPath, "utf-8")); //获取到所下载的资源 FileInputStream fis = new FileInputStream(path); int len = 0; byte [] buf = new byte[1024]; while((len=fis.read(buf))!=-1){ response.getOutputStream().write(buf,0,len); } }}download.html
<!DOCTYPE html><html><head><meta charset="ISO-8859-1"><title>Download Demo</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"></head><body><a href = "/JavabeanDemo/DownloadServlet">download</a></body></html>以上这篇Java Servlet简单实例分享(文件上传下载demo)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Java实现的文件上传下载工具类。分享给大家供大家参考,具体如下:这是一个在Eclipse环境下采用Java语言实现文件上传下载的工具类。和之前介
本文实例讲述了python实现的简单FTP上传下载文件的方法。分享给大家供大家参考。具体如下:python本身自带一个FTP模块,可以实现上传下载的函数功能。#
本文实例为大家分享了java文件上传下载的具体代码,供大家参考,具体内容如下文件上传@RequestMapping(value="/uploadFile",me
本文实例讲述了python实现支持目录FTP上传下载文件的方法。分享给大家供大家参考。具体如下:该程序支持ftp上传下载文件和目录、适用于windows和lin
本文实例为大家分享了nodejs实现文件上传下载的具体代码,供大家参考,具体内容如下1.介绍做了一个关于文件上传和下载的demo,选择了Multer作为中间件进