java实现⽂件上传(使⽤FromData)
⼀、使⽤FromData将表单对象序列化后,使⽤InputStream()来获取数据
1、表单代码如下
<form id="user-info" method="post" action="upload">
<input type="file" name="file" multiple>
<input type="text" name="username">
<input type="text" name="password">
</form>
<button id="sub">提交</button>
2、js的代码
<script>
function createXHR(){
return new XMLHttpRequest();
}
var sub = ElementById("sub");
var xhr = createXHR();
var form = ElementById("user-info");//获取上边的表单
xhr.open("post","upload",true);
xhr.send(new FormData(form));
}
</script>
3、由于是序列化的表单所以java后端,不能使⽤Parameter()获取数据,需要使⽤InputStream();获取数据⾸先我们看到获取的数据
  InputStream in = InputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = "";
while((adLine())!=null){
System.out.println(str);
}
可以看到读取到这些数据
4、接着使⽤apache的上传⽂件框架进⾏上传
实现效果
以下是代码
package com.wangyang.servlet;
inputtypefile不上传文件import java.io.File;
import java.io.IOException;
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.apachemons.fileupload.FileItem;
import org.apachemons.fileupload.FileUploadException;
import org.apachemons.fileupload.disk.DiskFileItemFactory;
import org.apachemons.fileupload.servlet.ServletFileUpload;
@WebServlet("/upload")
public class Upload extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
/*
InputStream in = InputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String str = "";
while((adLine())!=null){
System.out.println(str);
}
*/
System.out.println("[-------------------------------------------------------]");
String ServletContext().getRealPath("/")+"upload/";
File file = new File(filepath);
if(!ists()) {
file.mkdir();
}
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List<FileItem> items= upload.parseRequest(request);
for(FileItem item: items) {
System.out.println(Name());
if(!item.isFormField()) {
item.write(new File(Name()));
}
if(item.isFormField()){
System.out.String());
System.out.FieldName());
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.sendRedirect("index.jsp");
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta  charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Insert title here</title>
</head>
<body>
<form id="user-info" method="post" action="upload">
<input type="file" name="file" multiple>
<input type="text" name="username">
<input type="text" name="password">
</form>
<button id="sub">提交</button>
<script>
function createXHR(){
return new XMLHttpRequest();
}
var sub = ElementById("sub");
var xhr = createXHR();
var form = ElementById("user-info");
xhr.open("post","upload",true);
xhr.send(new FormData(form));
}
</script>
</body>
</html>
这⾥没有进⾏细节上的编程,只是为了说明简单原理

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。