怎么获取通过servlet 文件上传的文件
1个回答
展开全部
一、服务器端Servlet代码:
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
@WebServlet(name = "UploadTestServlet",urlPatterns = {"/upload"})
//下面的注解中location的值为上传的文件的临时存放位置
@MultipartConfig(location = "D://uploadtest")
public class UploadTestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameter("uploaderName"));
Part part=request.getPart("file"); //通过表单中的name属性获取表单域中的文件
part.write("D://uploadtest//aaa.txt"); //将文件移动到特定的磁盘路径
String fname=part.getSubmittedFileName();
System.out.println("文件名:"+fname);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
二、浏览器端HTML代码
版本1、普通表单提交
Servlet 通过表单上传文件和获取表单数据的最简单方式
<html>
<head>
<title>上传页</title>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data" method="post" action="/upload">
<input id="file" type="file" name="file"/>
<input name="uploaderName" type="text"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
版本2、AJAX异步提交表单
<html>
<head>
<script type="application/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<title>Title</title>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data" method="post" action="/upload">
<input id="file" type="file" name="file"/>
<input name="uploaderName" type="text"/>
<button id="upload" value="提交" type="button" onclick="fun();">upload</button>
</form>
<script type="application/javascript">
function fun() {
$.ajax({
url: '/upload',
type: 'POST',
cache: false,
data: new FormData($('#uploadForm')[0]),
processData: false,
contentType: false
}).done(function(res) {
}).fail(function(res) {});
}
</script>
</body>
</html>
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
@WebServlet(name = "UploadTestServlet",urlPatterns = {"/upload"})
//下面的注解中location的值为上传的文件的临时存放位置
@MultipartConfig(location = "D://uploadtest")
public class UploadTestServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getParameter("uploaderName"));
Part part=request.getPart("file"); //通过表单中的name属性获取表单域中的文件
part.write("D://uploadtest//aaa.txt"); //将文件移动到特定的磁盘路径
String fname=part.getSubmittedFileName();
System.out.println("文件名:"+fname);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
二、浏览器端HTML代码
版本1、普通表单提交
Servlet 通过表单上传文件和获取表单数据的最简单方式
<html>
<head>
<title>上传页</title>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data" method="post" action="/upload">
<input id="file" type="file" name="file"/>
<input name="uploaderName" type="text"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
版本2、AJAX异步提交表单
<html>
<head>
<script type="application/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<title>Title</title>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data" method="post" action="/upload">
<input id="file" type="file" name="file"/>
<input name="uploaderName" type="text"/>
<button id="upload" value="提交" type="button" onclick="fun();">upload</button>
</form>
<script type="application/javascript">
function fun() {
$.ajax({
url: '/upload',
type: 'POST',
cache: false,
data: new FormData($('#uploadForm')[0]),
processData: false,
contentType: false
}).done(function(res) {
}).fail(function(res) {});
}
</script>
</body>
</html>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询