Apache Commons fileUpload 구현 파일 업로드 중 하나
3557 단어 apachecommonsfileupload
두 개의jar 패키지가 필요합니다.
commons-fileupload.jar
Commons IO의jar 패키지(본고는commons-io-2.4.jar 사용)
Servlet을 사용하여 파일 업로드를 수행합니다.
package web.servlet;
import java.io.IOException;
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 java.io.File;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String uploadPath = "D:\\temp"; //
private String tempPath = "d:\\temp\\buffer\\"; //
File tempPathFile;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
// Create a factory for disk-based file items
DiskFileItemFactory factory = new DiskFileItemFactory();
// Set factory constraints
factory.setSizeThreshold(4096); // , 4kb
factory.setRepository(tempPathFile);//
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// Set overall request size constraint
upload.setSizeMax(4194304); // , 4MB
List<FileItem> items = upload.parseRequest(request);//
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
System.out.print("upload succeed");
} catch (Exception e) {
//
e.printStackTrace();
}
}
public void init() throws ServletException {
File uploadFile = new File(uploadPath);
if (!uploadFile.exists()) {
uploadFile.mkdirs();
}
File tempPathFile = new File(tempPath);
if (!tempPathFile.exists()) {
tempPathFile.mkdirs();
}
}
}
jsp
<%@ page language="java" contentType="text/html; charset=ISO--"
pageEncoding="utf-"%>
<!DOCTYPE html PUBLIC "-//WC//DTD HTML . Transitional//EN" "http://www.w.org/TR/html/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB">
<title>File upload</title>
</head>
<body>
<!-- // action="fileupload" web.xml <servlet-mapping> <url-pattern> . -->
<form name="myform" action="UploadServlet" method="post"
enctype="multipart/form-data">
File:<br>
<input type="file" name="myfile"><br>
<br>
<input type="submit" name="submit" value="Commit">
</form>
</body>
</html>
이렇게 하면 하나의 파일 업로드 기능을 간단하게 실현할 수 있다. 물론 이것은 가장 기본적이고 계속 연구 중이다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
프로덕션을 위한 SolrCloud 설정이 작업을 시작했을 때 SolrCloud에 익숙하지 않았습니다. 하지만 시간이 지나면서 요령이 생겼고 프로덕션 서버에서 SolrCloud를 설정하기 위한 최신 가이드를 작성하는 것이 좋은 생각이라고 생각합니다. 공식...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.