Java Web进阶(7) -- 文件上传及验证码

第一章 文件上传实现

1.1 原理分析
Java Web进阶(7) -- 文件上传及验证码_第1张图片

1.2 代码实现
需要jar包:commons-fileupload-1.2.1.jarcommons-io-1.4.jar
…\upload\WebContent下面创建upload文件夹


jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>文件上传</h1>
	<form action="${pageContext.request.contextPath }/UploadServlet" method="post" enctype="multipart/form-data">
		<input type="text" name="name"><br/>
		<input type="file" name="upload"><br/>
		<input type="submit" value="上传">
	</form>
</body>
</html>

servlet:

package com.imooc.servlet;

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.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons

你可能感兴趣的:(笔记,java)