gpt4 book ai didi

java - 表单参数未传递但处理了相同表单中的文件?

转载 作者:搜寻专家 更新时间:2023-10-31 20:11:19 25 4
gpt4 key购买 nike

<分区>

我在 JSP 上有一个网络表单,它有几个字符串值和一个通过 servlet 上传到服务器的文件。奇怪的是,我能够将文件上传到服务器,但无法使用 request.getParameter("someString") 获取 servlet 中的值。

我的代码有什么问题,或者指导我?

编辑:通过一些研究,我知道如果我在表单标签中使用 enctype="multipart/form-data",我将无法使用 在 servlet 中获取参数请求.getParameter()。现在的问题可能是,如何将文件和其他值发送到 servlet 进行处理。

webform.jsp:

<form method="POST"  enctype="multipart/form-data" action="/cassino/uploadFile" >
<fieldset>
<div class="form-group">
<label >*ID riparazione</label>

<input type="text" name="idRiparazione" />
</div>
<div class="form-group">
<label>*ID mandrino smontato</label>

<input type="text" name="idMandrinoSmontato" />
</div>
<div class="form-group">
<label>*Service livello(SL)</label>
<input type="text" name="serviceLivello" />
</div>
<div class="form-group">
<label>Attachment</label>
<input type="file" name="attachment" class="" id="attach" />
</div>
</fieldset>
</div>
<p class="text-right">
<input type="submit" value="Salva" name="newMacchina" />
<input type="reset" value="Cancella" />
</p>
</form>

uploadFile.java

@WebServlet( name = "uploadFile", urlPatterns = { "/uploadFile" } )
public class uploadFile extends HttpServlet {
private static final long serialVersionUID = 1L;

private static final int THRESHOLD_SIZE = 1024 * 1024 * 3;
private static final int MAX_FILE_SIZE = 1024 * 1024 * 15;
private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 20;

/**
* handles file upload via HTTP POST method
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

// checks if the request actually contains upload file
//ServletFileUpload.isMultipartContent(request);

String idRiparazione = request.getParameter("idRiparazione");
String idMandrinoSmontato = request.getParameter("idMandrinoSmontato");
String serviceLivello = request.getParameter("serviceLivello");

PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("<h1>Hello Servlet Get</h1>");
out.println(idMandrinoSmontato);
out.println(serviceLivello);
out.println("</body>");
out.println("</html>");

DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(THRESHOLD_SIZE);
factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(MAX_FILE_SIZE);
upload.setSizeMax(MAX_REQUEST_SIZE);

// constructs the directory path to store upload file
String uploadPath = "C:\\SKFSFCassino\\uploads\\riparazione\\"; //getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY;
// creates the directory if it does not exist
File uploaddir = new File(uploadPath);
if( !uploaddir.exists()) {
uploaddir.mkdirs();
}

try {
List formItems = upload.parseRequest(request);
Iterator it = formItems.iterator();
// iterates over form's fields
while(it.hasNext()) {
FileItem item = (FileItem) it.next();
// processes only fields that are not form fields
if (!item.isFormField()) {
String fileName = new File(item.getName()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// saves the file on disk
item.write(storeFile);
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

// response.sendRedirect(request.getContextPath()+"/modules/SKFsfRepair/new/viewNewContent.jsp");

}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
throw new ServletException("GET method used with " + getClass().getName() + ": POST method required.");
}

我认为映射不会有问题,但我对此也有点困惑,因为我的 web.xml 没有显示任何映射或 servlet。但是,servlet 会上传文件并返回以下输出:

Hello Servlet Get

null null

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com