Java Servlet上传文件

在网上看好多程序员在找Java文件上传的demo,其实用 Servlet3.0( JDK1.6)自身带的API就可以了,Servlet3.0新增了Part接口,HttpServletRequest的getPart()方法取得Part实现对象,以下是实现单文件上传的代码:
1、前端页面代码:
<!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>调用HttpServletRequest的getPart()方法!</h1><br>
<form action="OneFileUpLoadServlet" method="post" enctype="multipart/form-data">
选择一个要上传的文件:<br>
<input type="file" name="one-photo"/><br>
<input type="submit" name="upload" value="Upload">
</form>
</body>
</html>

2、后台Servlet代码:
package com.victory.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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;

@MultipartConfig
@WebServlet("/OneFileUpLoadServlet")
public class OneFileUpLoadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doPost(request, response);

}
   
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

Part part = request.getPart("one-photo");
String fileName = getFileName(part);
writeTo(fileName, part);

}

private String getFileName(Part part) throws IOException{
String header = part.getHeader("Content-Disposition");
String filePath = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\""));
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);

return fileName;
}

//保存到服务器上的C:根目录下
private void writeTo(String fileName, Part part) throws IOException {
InputStream is = part.getInputStream();
OutputStream os = new FileOutputStream("C:/"+fileName);
byte[] buffer = new byte[1024*2];
int length = -1;
while((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
is.close();
os.close();
}

}

以下是实现多文件上传:
1、前端页面:
<!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>调用HttpServletRequest的getParts()方法!</h1><br>
<form action="MoreFileUpLoadServlet" method="post" enctype="multipart/form-data">
选择要上传的文件:<br>
<input type="file" name="filename1" value="" /><br>
<input type="file" name="filename2" value="" /><br>
<input type="file" name="filename3" value="" /><br>
<input type="file" name="filename4" value="" /><br>
<input type="file" name="filename5" value="" /><br>
<input type="file" name="filename6" value="" /><br>
<input type="file" name="filename7" value="" /><br>
<input type="file" name="filename8" value="" /><br>
<input type="submit" name="upload" value="Upload">
</form>
</body>
</html>

2、后台Servlet代码:
package com.victory.servlet;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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;

@MultipartConfig
@WebServlet("/MoreFileUpLoadServlet")
public class MoreFileUpLoadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

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

request.setCharacterEncoding("UTF-8");

for(Part part:request.getParts()) {
if(part.getName().startsWith("file")) {
String fileName = getFileName(part);
writeTo(fileName, part);
}
}
}

private String getFileName(Part part) throws IOException{
String header = part.getHeader("Content-Disposition");
String filePath = header.substring(header.indexOf("filename=\"") + 10, header.lastIndexOf("\""));
String fileName = filePath.substring(filePath.lastIndexOf("\\")+1);

return fileName;
}

//保存到服务器上的C:根目录下
private void writeTo(String fileName, Part part) throws IOException {
InputStream is = part.getInputStream();
OutputStream os = new FileOutputStream("C:/"+fileName);
byte[] buffer = new byte[1024*2];
int length = -1;
while((length = is.read(buffer)) != -1) {
os.write(buffer, 0, length);
}
is.close();
os.close();
}

}

附件是demo的详细代码,希望对大家有用,谢谢!

你可能感兴趣的:(java,servlet上传文件)