8 Mart 2019 Cuma

ServletFileUpload Sınıfı - Kullanmayın

Giriş
Şu satırı dahil ederizz
import org.apache.commons.fileupload.servlet.ServletFileUpload;
Servlet'in doPost() metodunda upload edilen dosyaları diske yazmak için kullanılır. Ancak artık kullanmamak lazım. Açıklaması şöyle
commons-fileupload is not required from Servlet 3.1 onwards.

In fact, using commons-fileupload in combination with the a container that supports Servlet spec 3.1 (or newer) is actually not recommended.

There hasn't even been a release of commons-fileupload since 2018, and no releases that support Servlet 3.1 or newer (the last release of commons-fileupload supports Servlet 2.4 and older)

Why?

The Multipart features are built into the Servlet spec since 3.1.

Every server that supports Servlet 3.1 supports multipart file upload now.

That includes Jetty 9.

Use HttpServletRequest.getPart() API in code. You configure it via either the @MultipartConfig annotation and/or the <multipart-config> descriptor element in your WEB-INF/web.xml
constructor - DiskFileItemFactory 
Şöyle yaparız.
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(MEMORY_THRESHOLD);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

ServletFileUpload upload = new ServletFileUpload(factory);
parseRequest metodu
Şöyle yaparız.
ServletFileUpload upload = ...

try {
  @SuppressWarnings("unchecked")
  List<FileItem> formItems = upload.parseRequest(request); //empty list
  ...
}
setFileSizeMax metodu
Şöyle yaparız.
upload.setFileSizeMax(MAX_FILE_SIZE);
setSizeMax metodu
Şöyle yaparız.
upload.setSizeMax(MAX_REQUEST_SIZE);

Hiç yorum yok:

Yorum Gönder