JAVA maven apache commons-fileupload: как перейти с версии 1.5 на версию 1.6.0JAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 JAVA maven apache commons-fileupload: как перейти с версии 1.5 на версию 1.6.0

Сообщение Anonymous »

[МОТИВАЦИЯ]
У меня есть проект maven, который использует commons-fileupload 1.5. Когда я хочу обновить зависимость до 1.6.0, она не компилируется. здесь
[ОГРАНИЧЕНИЕ]
Я не могу использовать библиотеки jakarta в коде из-за более поздней зависимости (org.gwtproject).
[МИГРАЦИЯ В ФАЙЛАХ КОНФИГУРАЦИИ]
@pom.xml:

commons-fileupload
commons-fileupload


commons-io
commons-io


1.6.0


@module-info.java:
requires org.apache.commons.fileupload;//commons.fileupload;

[ОШИБКИ КОМПИЛЯЦИИ]
cd C:\git\blg\com.tugalsan.blg.org_apache_commons_fileupload_1_6_0; JAVA_HOME=C:\\bin\\java\\home cmd /c "\"C:\\bin\\netbeans\\home\\java\\maven\\bin\\mvn.cmd\" -Dmaven.ext.class.path=C:\\bin\\netbeans\\home\\java\\maven-nblib\\netbeans-eventspy.jar --no-transfer-progress clean install"

Required filename-based automodules detected: [javax.servlet-api-4.0.1.jar]. Please don't publish this project to a public artifact repository!

COMPILATION ERROR :
-------------------------------------------------------------
com/tugalsan/blg/org_apache_commons_fileupload_1_6_0/server/TS_SUploadWebServlet.java:[113,51] cannot access javax.servlet.ServletContextListener
class file for javax.servlet.ServletContextListener not found
com/tugalsan/blg/org_apache_commons_fileupload_1_6_0/server/TS_SUploadWebServlet.java:[48,35] cannot access javax.servlet.http.HttpServletRequest
class file for javax.servlet.http.HttpServletRequest not found
com/tugalsan/blg/org_apache_commons_fileupload_1_6_0/server/TS_SUploadWebServlet.java:[55,73] no suitable method found for parseRequest(javax.servlet.http.HttpServletRequest)
method org.apache.commons.fileupload.FileUploadBase.parseRequest(org.apache.commons.fileupload.RequestContext) is not applicable
(argument mismatch; javax.servlet.http.HttpServletRequest cannot be converted to org.apache.commons.fileupload.RequestContext)
method org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(javax.servlet.http.HttpServletRequest) is not applicable
(argument mismatch; javax.servlet.http.HttpServletRequest cannot be converted to javax.servlet.http.HttpServletRequest)
3 errors

[ВОПРОС]
Какие изменения кода следует применить для полной миграции.
[ДОПОЛНИТЕЛЬНЫЕ ПРИМЕЧАНИЯ]
в проектах netbeans с зависимостями, javax.servlet-api-4.0.1.jar и commons-fileupload-1.6.0.jar & commons-io-2.19.0.jar существует.


[ФАЙЛЫ MRE]

pom.xml


4.0.0
com.tugalsan
com.tugalsan.blg.org_apache_commons_fileupload_1_6_0
1.0-SNAPSHOT


javax.servlet
javax.servlet-api
4.0.1
provided


commons-fileupload
commons-fileupload


commons-io
commons-io


1.6.0


commons-io
commons-io
2.19.0






src/main/java

**/*.java
**/*.gwt.xml



src/main/resources

**/*.*







org.apache.maven.plugins
maven-source-plugin
3.3.1


attach-sources
package

jar-no-fork








org.apache.maven.plugins
maven-compiler-plugin
3.13.0

24
true







UTF-8



module-info.java
module com.tugalsan.blg.org_apache_commons_fileupload_1_6_0 {
requires javax.servlet.api;

// requires commons.fileupload;
requires org.apache.commons.fileupload;//;

}

TS_SUploadWebServlet.java
package com.tugalsan.blg.org_apache_commons_fileupload_1_6_0.server;

import javax.servlet.http.*;
import java.io.File;
import static java.lang.System.out;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebListener;
import javax.servlet.annotation.WebServlet;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

@WebServlet("/u")
@MultipartConfig(
fileSizeThreshold = 1024 * 1024 * TS_SUploadWebServlet.UPLOAD_MB_LIMIT_MEMORY,
maxFileSize = 1024 * 1024 * TS_SUploadWebServlet.UPLOAD_MB_LIMIT_FILE,
maxRequestSize = 1024 * 1024 * TS_SUploadWebServlet.UPLOAD_MB_LIMIT_REQUESTBALL,
location = "/" + TS_SUploadWebServlet.UPLOAD_DIR_NAME//means C:/bin/tomcat/home/work/Catalina/localhost/spi-xxx/upload (do create it)
)
public class TS_SUploadWebServlet extends HttpServlet {

final public static String UPLOAD_DIR_NAME = "p";
final public static int UPLOAD_MB_LIMIT_MEMORY = 10;
final public static int UPLOAD_MB_LIMIT_FILE = 25;
final public static int UPLOAD_MB_LIMIT_REQUESTBALL = 50;

@Override
public void doGet(HttpServletRequest rq, HttpServletResponse rs) {
call(this, rq, rs);
}

@Override
protected void doPost(HttpServletRequest rq, HttpServletResponse rs) {
call(this, rq, rs);
}

public static void call(HttpServlet servlet, HttpServletRequest rq, HttpServletResponse rs) {
try {
var appPath = rq.getServletContext().getRealPath("");// constructs path of the directory to save uploaded file
var savePath = appPath + File.separator + TS_SUploadWebServlet.UPLOAD_DIR_NAME;// creates the save directory if it does not exists
var fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}

if (!ServletFileUpload.isMultipartContent(rq)) {
println(rs, "USER_NOT_MULTIPART");
return;
}

//GETING ITEMS
//WARNING: Dont touch request before this, like execution getParameter or such!
var items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(rq);

//GETTING PROFILE
var profile = items.stream().filter(item -> item.isFormField()).findFirst().orElse(null);
if (profile == null) {
println(rs, "RESULT_UPLOAD_USER_PROFILE_NULL");
return;
}
println(rs, "profile_selected");

var profileValue = profile.getString();
if (profileValue == null) {
println(rs, "RESULT_UPLOAD_USER_PROFILEVALUE_NULL");
return;
}
println(rs, "profileValue: " + profileValue);

//GETING SOURCEFILE
var sourceFile = items.stream().filter(item -> !item.isFormField()).findFirst().orElse(null);
if (sourceFile == null) {
println(rs, "RESULT_UPLOAD_USER_SOURCEFILE_NULL");
return;
}
println(rs, "sourceFile_selected");

var sourceFileName = sourceFile.getName();
if (sourceFileName == null) {
println(rs, "RESULT_UPLOAD_USER_SOURCEFILENAME_NULL");
return;
}
println(rs, "sourceFileName: " + sourceFileName);

//COMPILING TARGET FILE
var targetFile = fileSaveDir.toPath().resolve(profileValue).resolve(sourceFileName);

//CREATE DIRECTORIES
targetFile.getParent().toFile().mkdirs();

//STORE FILE
Files.createFile(targetFile);
sourceFile.write(targetFile.toFile());

//RETURN SUCCESS FLAG
rs.setStatus(HttpServletResponse.SC_CREATED);
println(rs, "RESULT_UPLOAD_USER_SUCCESS");
} catch (Exception e) {
throwIfInterruptedException(e);
e.printStackTrace();
}
}

private static void println(HttpServletResponse rs, String msg) {
try {
out.println("println: " + msg);
rs.getWriter().println(msg);
} catch (Exception e) {
throwIfInterruptedException(e);
}
}

//---------------------------- LISTENER ----------------
@WebListener
public class ApacheFileCleanerCleanup extends FileCleanerCleanup {

}

//----------------------------- UTILS -----------------------
@SuppressWarnings("unchecked")
private static void _throwAsUncheckedException(Throwable exception) throws T {
throw (T) exception;
}

@Deprecated //only internalUse
private static void throwAsUncheckedException(Throwable exception) {
TS_SUploadWebServlet._throwAsUncheckedException(exception);
}

public static R throwIfInterruptedException(Throwable t) {
if (isInterruptedException(t)) {
Thread.currentThread().interrupt();
throwAsUncheckedException(t);
}
return null;
}

public static boolean isInterruptedException(Throwable t) {
if (t instanceof InterruptedException) {
return true;
}
if (t.getCause() != null) {
return isInterruptedException(t.getCause());
}
return false;
}
}


Подробнее здесь: https://stackoverflow.com/questions/796 ... 5-to-1-6-0
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»