GetPart() должен требовать POST, а другие требуют GETJAVA

Программисты JAVA общаются здесь
Гость
GetPart() должен требовать POST, а другие требуют GET

Сообщение Гость »


I'm new to build a web project and I have a problem. I found that if I use GET method, the servlet will reponse the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is null but if I use POST method, that problem seems to be disapeared but I get HTTP method POST is not supported by this URL. This is my JSP code:

Name: Trailer Link: Categories:
${category.categoryName}
Tags:
${tag.tagName} Description: Thumbnail: Submit This is my servlet Code:

@WebServlet(name = "addFilmControl", urlPatterns = {"/addFilm"}) @MultipartConfig(location = "C:\\Users\\tmtmt\\IdeaProjects\\AnimeFilmWeb\\thumbnailUpload") public class addFilmControl extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); String trailerLink = req.getParameter("trailerLink"); String[] categories = req.getParameterValues("categories"); String[] tags = req.getParameterValues("tags"); String description = req.getParameter("description"); Part filePart = req.getPart("thumbnail"); String fileName = filePart.getSubmittedFileName(); Timestamp releaseDate = new Timestamp(new Date().getTime()); String savePath = getServletContext().getRealPath("") + File.separator + fileName; File fileSaveDir = new File(savePath); filePart.write(savePath + File.separator); filmDao dao = new filmDao(); categoryDao categoryDao = new categoryDao(); tagsDao tagsDao = new tagsDao(); Film film = new Film(); film.setFilmName(name); film.setFilmTrailerLink(trailerLink); film.setFilmDescription(description); film.setFilmReleaseDate(releaseDate); film.setFilmImgLink(fileName); film.setFilmViewCount(0L); // Add film to DB boolean sucess = dao.addFilm(film); int filmId = film.getFilmID(); if (sucess) { // Add categories for film for (String categoryId : categories) { categoryDao.insertCategory(filmId, Integer.parseInt(categoryId)); } // Add tags for film for (String tagId : tags) { tagsDao.insertTags(filmId, Integer.parseInt(tagId)); } req.setAttribute("successMessage", "Film added successfully"); req.getRequestDispatcher("newFilmPage").forward(req, resp); } else { System.out.println("Film added failed"); req.setAttribute("errorMessage", "Film added failed"); req.getRequestDispatcher("newFilmPage").forward(req, resp); } } } I tried searching but nothing mentioned this issue. And even GPT 4 responsed that GET or POST response also doesn't affect getPart much. Hope someone helps me!


Источник: https://stackoverflow.com/questions/780 ... equire-get

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