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, она не компилируется. Здесь < /p>
[ограничение]

i не могу использовать библиотеки Jakarta на коде, из -за более поздней зависимости (org.gwtproject). < /p>
[Миграция в файлах конфигурации]

< /p>
Class = "Lang-Java PrettyPrint-Override">
commons-fileupload
commons-fileupload


commons-io
commons-io


1.6.0

< /code>

@module-info.java
requires org.apache.commons.fileupload;//commons.fileupload;
< /code>

[ошибки компиляции]
cd C:\git\blg\com.tugalsan.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"

--- compiler:3.13.0:compile (default-compile) @ com.tugalsan.org_apache_commons_fileupload_1_6_0 ---
Required filename-based automodules detected: [javax.servlet-api-4.0.1.jar]. Please don't publish this project to a public artifact repository!
Recompiling the module because of changed source code.
Compiling 5 source files with javac [debug preview release 24 module-path] to target\classes

-------------------------------------------------------------
COMPILATION ERROR :
-------------------------------------------------------------
com/tugalsan/blg/org_apache_commons_fileupload_1_6_0/server/core/ApacheFileCleanerCleanup.java:[7,47] 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:[46,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:[53,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



[QUESTION]

What code changes should I apply to migrate fully.

[ADDITIONAL NOTES]

on netbeans on projects on dependecies, javax.servlet-api-4.0.1.jar and commons-fileupload-1.6.0.jar exists.

[mre files]

pom.xml


4.0.0
com.tugalsan
com.tugalsan.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


< /code>
module info.java
module com.tugalsan.org_apache_commons_fileupload_1_6_0 {
requires javax.servlet.api;

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

}
< /code>
apachefilecleanercleanup.java
package com.tugalsan.blg.org_apache_commons_fileupload_1_6_0.server.core;

import javax.servlet.annotation.WebListener;
import org.apache.commons.fileupload.servlet.FileCleanerCleanup;

@WebListener
public class ApacheFileCleanerCleanup extends FileCleanerCleanup {

}
< /code>
ts_suploadwebservlet.java
package com.tugalsan.blg.org_apache_commons_fileupload_1_6_0.server;

import com.tugalsan.blg.org_apache_commons_fileupload_1_6_0.client.*;
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.WebServlet;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
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 = "/" + TGS_SUploadUtils.LOC_NAME//means C:/bin/tomcat/home/work/Catalina/localhost/spi-xxx/upload (do create it)
)
public class TS_SUploadWebServlet extends HttpServlet {

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 + TGS_SUploadUtils.LOC_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, TGS_SUploadUtils.RESULT_UPLOAD_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, TGS_SUploadUtils.RESULT_UPLOAD_USER_PROFILE_NULL());
return;
}
println(rs, "profile_selected");

var profileValue = profile.getString();
if (profileValue == null) {
println(rs, TGS_SUploadUtils.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, TGS_SUploadUtils.RESULT_UPLOAD_USER_SOURCEFILE_NULL());
return;
}
println(rs, "sourceFile_selected");

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

//COMPILING TARGET FILE
var targetFile = Path.of(profileValue);

//CREATE DIRECTORIES
targetFile.getParent().toFile().mkdirs();
Files.createFile(targetFile);
sourceFile.write(targetFile.toFile());
rs.setStatus(HttpServletResponse.SC_CREATED);
println(rs, TGS_SUploadUtils.RESULT_UPLOAD_USER_SUCCESS());
} catch (Exception e) {
TGS_FuncUtils.throwIfInterruptedException(e);
e.printStackTrace();
}
}

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


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

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