1
我可以在Java Web應用程序中上傳文件,但是,如果我想發送信息並上傳文件,則系統上載文件並將信息視爲空!如何上傳文件並使用POST在Java Web應用程序中發送文本平面信息
我的代碼:
的Servlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("information") == null) System.out.println("information is null"); Helper helper = new Helper(); String url = "C:\\Users\\.....<private url>"; DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(1024); factory.setRepository(new File(url)); ServletFileUpload upload = new ServletFileUpload(factory); try{ List<FileItem> partes = upload.parseRequest(request); for(FileItem items: partes){ File file = new File(url,items.getName()); items.write(file); request.getRequestDispatcher("view/upfile/upfile.jsp").forward(request, response); return; } }catch(Exception e){ request.getSession().setAttribute("error", e.toString() + ": " + url); request.getRequestDispatcher("view/excepciones/message.jsp").forward(request, response); return; }
}
文件JSP:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Subir el archivo</h1>
<form action = "<%= request.getContextPath() %>/UpFilesController" method="post" enctype = "multipart/form-data">
<input type = "file" name = "file"/>
<input type = "text" name = "information"/>
<input type = "submit" name = "action4" value = "Subir Archivo"/>
</form>
</body>
利用GlassFish服務器: 信息爲空
這個問題已經在這裏找到答案:http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet –