gpt4 book ai didi

java - 如何从 HTML 中的 java servlet 中的 fileupload 获取完整文件路径

转载 作者:行者123 更新时间:2023-12-02 04:02:09 25 4
gpt4 key购买 nike

我怎样才能从servlet中的HTML获取文件上传的完整路径来上传它。当我打印要上传的文件名时,它只是获取名称而不是完整路径。我不知道如何获取它。我收到此错误。

fileField!!!!!!!!  Browse 
fileName!!!!!!!! fromJSON.csv

Jan 14, 2016 7:31:51 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [PriorityServlet] in context with path[/tc-eqcweb] threw exception
java.io.FileNotFoundException: fromJSON.csv (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.mongodb.gridfs.GridFS.createFile(GridFS.java:239)
at com.eqc.servlet.PriorityServlet.doPost(PriorityServlet.java:107)

My HTML form to upload the file

<form action="priorityServlet" method="post" enctype="multipart/form-data">
<div class="col-sm-8 policytxt_align">
<div class="file_1_mas">
<a class='btn btn-primary' href='javascript:;'>
<input id="file_bal" name="Browse" type="file" value="Browse" />
</a> &nbsp; <br />
<div><input type="submit" value="Upload File" onClick="getExclusion();" /> </div>

<div align="center"></div>
<div></div>

</div>

My servlet is not getting the full path of file to upload:

public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
isMultipart = ServletFileUpload.isMultipartContent(req);
java.io.PrintWriter out = res.getWriter();
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(maxMemSize);
factory.setRepository(new File("D:\\temp111"));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(maxFileSize);
String fileName = null;
String fieldName = null;
String rawName = null;
try {

List fileItems = upload.parseRequest(req);
Iterator i = fileItems.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
if (!fi.isFormField()) {
// Get the uploaded file parameters
fieldName = fi.getFieldName();
fileName = fi.getName();

}
}
System.out.println("fileField!!!!!!!! "+fieldName);
System.out.println("fileName!!!!!!!! "+fileName);
} catch (Exception ex) {
System.out.println(ex);
}

最佳答案

您忘记了 html 代码中的 enctype 属性

<input id="file_bal" name="Browse" type="file" value="Browse" />

改成这个

<input id="file_bal" name="Browse" type="file" value="Browse" enctype="multipart/form-data"/>

只有这样你才能使用getPart读取它

@ScaryWombat 在他的帖子中指出,您无法获取文件路径,并且不需要它来上传文件,因为您正在使用 getPart() 读取 servlet 中的文件方法

希望对大家有帮助

关于java - 如何从 HTML 中的 java servlet 中的 fileupload 获取完整文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34780839/

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