[英]my own String to Integer (atoi) function doesn't work correctly
[英]Parsing string value to integer doesn't work
我對某些未提供預期響應的代碼有疑問:
Intellij/Payara 提供此錯誤:
[2019-10-26T21:26:35.875+0200] [Payara 5.193] [警告] [] [javax.enterprise.web] [tid: _ThreadID=30 _ThreadName=http-thread-pool::http-listener-1( 3)] [timeMillis: 1572117995875] [levelValue: 900] [[ StandardWrapperValve[FileUploadServlet]: Servlet.service() for servlet FileUploadServlet threw exception java.lang.NullPointerException at FileUploadServlet.doPost(FileUploadServlet.java:27)
特定的行號包含將字符串數據類型解析為 int 的代碼。
public class FileUploadServlet extends HttpServlet {
String result = "";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
HttpSession file = request.getSession(true);
int uploadcode = Integer.parseInt(request.getParameter("uploadcode"));
int code = 1111;
boolean checkcode = check(uploadcode, code);
if ((checkcode && ((uploadcode != 0))){
try {
Part filePart = request.getPart("fileToUpload");
InputStream fileInputStream = filePart.getInputStream();
File fileToSave = new File("filepath" +filePart.getSubmittedFileName());
Files.copy(fileInputStream, fileToSave.toPath(), StandardCopyOption.REPLACE_EXISTING);
result = "File send and saved";
file.setAttribute("Bericht", result);
} catch (Exception ex){
result = "File not sent, please try again.";
file.setAttribute("Bericht", result);
} finally{
result = "File sent and saved";
file.setAttribute("Bericht", result);
getServletContext().getRequestDispatcher("/Upload.jsp").forward(request, response);
}
} else {
result = "incorrect upload code.";
file.setAttribute("Bericht", result);
getServletContext().getRequestDispatcher("/Upload.jsp").forward(request, response);
}
}
public boolean check(int a, int b) {
return a == b;
}
}
我希望代碼與輸入匹配並將文件保存到磁盤。 任何指針? 謝謝:)
問題可能是形式?
<form id="form1" enctype="multipart/form-data" method="post"
action="FileUploadServlet">
<div id="fileName"></div>
<div id="fileSize"></div>
<div id="fileType"></div>
<input type="number" name="uploadcode" id="uploadcode"
placeholder="Enter upload code" required>
<div>
Select the file to upload:
<br>
<input type="file" name="fileToUpload" id="fileToUpload"
accept="image/*" />
<br>
<br>
<ul class="actions">
<li>
<input type="submit" class="button alt"
value="Send Message" />
</li>
</ul>
</div>
<div id="progressNumber"></div>
<div id="text"></div>
</form>
或者它可能是 java 腳本上傳:
function uploadFile() {
var fd = new FormData();
fd.append("fileToUpload", document.getElementById('fileToUpload').files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
xhr.open("POST", "FileUploadServlet");
xhr.send(fd);
}
不幸的是(26-10-2019 23:10),不同的錯誤:
[2019-10-26T22:57:54.019+0200] [Payara 5.193] [WARNING] [] [javax.enterprise.web] [tid: _ThreadID=29 _ThreadName=http-thread-pool::http-listener-1(2)] [timeMillis: 1572123474019] [levelValue: 900] [[
StandardWrapperValve[FileUploadServlet]: Servlet.service() for servlet FileUploadServlet threw exception
java.lang.NumberFormatException: For input string: ""
在 servlet 之后,我使用這些將變量傳遞給 JSP:
<%
HttpSession file = request.getSession(false);
String resultaat= (String)file.getAttribute("Bericht");
%>
和:
<%out.println(resultaat);%>
通過設置正確的文件屬性並刪除finally語句解決,不正確使用兩態boolean。
傳入的請求根本不包含名為uploadcode
的參數。 這意味着request.getParameter()
調用返回null
,並試圖通過Integer.parseInt
將其轉換為 int 反過來會產生NullPointerException
。
因此,您輸入了uploadcode
,或者您的 javascript 和/或 HTML 中存在錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.