我有两个servlet的Java应用程序。第一个servlet将上传文件。第二个是检索上传的文件。
我可以上传文件并将其转发到第二个servlet。但是,当我试图检索th文件获取空指针异常。如何在第二个servlet中检索文件属性。
我尝试从Serv1转发到Serv2以检索上传的文件
Serv1.java:
private static final String UPLOAD_DIR = "../AISNMEA/WebContent/WEB-INF/resources";
public String file1;
String filePath;
String uploadFilePath;
UploadFile upfile;
String fileName;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
// set location for saving uploaded file
UploadBean upb = new UploadBean();
upb.setFolderstore(UPLOAD_DIR);
upb.setFilesizelimit(1073741824);
upb.setOverwrite(true);
MultipartFormDataRequest nreq = new MultipartFormDataRequest(
request);
// completed file uploading
upb.store(nreq);
Hashtable<?, ?> ht = nreq.getFiles();// gives the uploaded file
Enumeration<?> e = ht.elements();
while (e.hasMoreElements()) {
upfile = (UploadFile) e.nextElement();
uploadFilePath = UPLOAD_DIR + File.separator;
File fileSaveDir = new File(uploadFilePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdirs();
}
file1 = UPLOAD_DIR + File.separator + upfile.getFileName();
filePath = new File(file1).getAbsolutePath();
fileName = upfile.getFileName()
.substring(0, upfile.getFileName().indexOf("."))
.replaceAll("\\s+", "").trim().concat("_")
.concat("data");
}
ServletContext sc = getServletContext();
HttpSession session=request.getSession();
session.setAttribute("fileName", filePath);
RequestDispatcher rd = sc.getRequestDispatcher("/vdr.html");
rd.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
Serv2.java:
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
double base_x = Double.parseDouble(req.getParameter("n1"));
double base_y = Double.parseDouble(req.getParameter("n2"));
double m0 = Double.parseDouble(req.getParameter("n3"));
double a = Double.parseDouble(req.getParameter("n4"));
double b = Double.parseDouble(req.getParameter("n5"));
double k1 = Double.parseDouble(req.getParameter("n6"));
double k2 = Double.parseDouble(req.getParameter("n7"));
double k3 = Double.parseDouble(req.getParameter("n8"));
double k4 = Double.parseDouble(req.getParameter("n9"));
double k5 = Double.parseDouble(req.getParameter("n10"));
double k6 = Double.parseDouble(req.getParameter("n11"));
double k7 = Double.parseDouble(req.getParameter("n12"));
double k8 = Double.parseDouble(req.getParameter("n13"));
double Lbp = Double.parseDouble(req.getParameter("n14"));
double ido00 = base_x * PI / 180.0;
double keido00 = base_y * PI / 180.0;
double s0 = k1 * ido00 + k2 * Math.sin(2.0 * ido00) + k3
* Math.sin(4.0 * ido00) + k4 * Math.sin(6.0 * ido00) + k5
* Math.sin(8.0 * ido00) + k6 * Math.sin(10.0 * ido00) + k7
* Math.sin(12.0 * ido00) + k8 * Math.sin(14.0 * ido00);
double f = 1.0 / 299.152813;
double e2 = (Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(a, 2);
HttpSession session=req.getSession();
filePath = (String) session.getAttribute("fileName");
System.out.println("File Path" + filePath);
doGet(req, res);
}
的index.html:
<form action="ser1" method="post" enctype="multipart/form-data" onsubmit="return Validate(this);">
<input name="fileName" type="file"/>
</form>
vdr.html:
<form name="myForm" action="ser2" method="post"
onsubmit="return Validate(this);">
</form>
先谢谢
答案 0 :(得分:2)
您正在保存file name in request scope
,因此它仅在vdr.html
中可用,而不在Serv2.java
中。
如果您保存file name in session scope
,则文件名将在session
中可用。
你必须这样做,
发件人,
request.setAttribute("fileName", "file");
要,强>
session.setAttribute("fileName", "file");
然后你可以得到文件名,
String filePath = (String) session.getAttribute("fileName");
还有一个额外的信息,
Servlet不是thread-safe。所以请尝试在doGet()和doPost()中声明变量。这是一个很好的做法。
希望它有所帮助。