When I learn how to use servlet to achieve file uploading, I met this problem. details are as follows, my full code:
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import java.io.*;
import java.util.List;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
// set Content-Type
resp.setHeader("Content-type", "text/html;charset=UTF-8");
// create DiskFileItemFactory object
DiskFileItemFactory factory = new DiskFileItemFactory();
// set file temporary storage path, if the path does not exist, create it
factory.setRepository(new File("D:\\source\\jsp\\fileupdown\\temp"));
if (!factory.getRepository().exists()) {
factory.getRepository().mkdirs();
}
// set ServletFileUpload object
ServletFileUpload upload = new ServletFileUpload(factory);
// set character encoding
upload.setHeaderEncoding("UTF-8");
// analyze request, get file item list
List<FileItem> fileitems = upload.parseRequest(req);
// get byte stream
PrintWriter writer = resp.getWriter();
// traverse file item list
for (FileItem fileitem : fileitems) {
// if it is a form field
if (fileitem.isFormField()) {
// get field name
String name = fileitem.getFieldName();
if (name.equals("name")) {
if (fileitem.getString("UTF-8").equals("")) {
writer.println("name can not be empty");
} else {
writer.println("name:" + fileitem.getString("UTF-8"));
}
}
} else {
// get file name
String filename = fileitem.getName();
// process file
if (filename != null && !filename.equals("")) {
writer.print("uploaded file:" + filename + "<br>");
filename = filename.substring(filename.lastIndexOf("\\") + 1);
// unique file name
String uniqueFileName = System.currentTimeMillis() + "_" + filename;
// create same name file in server
String webPath = "/upload/";
String realPath = getServletContext().getRealPath(webPath + uniqueFileName);
// create file
File file = new File(realPath);
file.getParentFile().mkdirs();
file.createNewFile();
// get UploadStream
InputStream in = fileitem.getInputStream();
// use FileOutputStream to write file
FileOutputStream out = new FileOutputStream(file);
// create buffer
byte[] buffer = new byte[1024];
int len = 0;
// read file
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
in.close();
out.close();
writer.println("upload successfully");
} else {
writer.println("file can not be null");
}
}
}
} catch (FileUploadException e) {
throw new RuntimeException(e);
}
}
}
the error:
java: Cannot access javax.servlet.http.HttpServletRequest
Cannot find class file of javax.servlet.http.HttpServletRequest
the code that reports the error:
// set ServletFileUpload object
ServletFileUpload upload = new ServletFileUpload(factory);
// set character encoding
upload.setHeaderEncoding("UTF-8");
// analyze request, get file item list
List<FileItem> fileitems = upload.parseRequest(req);
I have tried to rewrite the code as follows:
// set ServletFileUpload object
ServletFileUpload upload = new ServletFileUpload(factory);
// set character encoding
upload.setHeaderEncoding("UTF-8");
// analyze request, get file item list
List<FileItem> fileitems = upload.parseRequest((RequestContext) req);
but it still cannot work.
I think the error comes from the difference between package javax and jakarta. The acticles teching how to use servlet to upload file use the old version servlet in javax while i use servlet in jakarta. However, I searched for a long time on the Internet and could not find a way to solve the error without changing the jakarta to javax.
Thx for your help.