我正在上传一个文件并将其显示为链接...但最初当我加载页面时它显示一个空值...我想删除这个空值......
<form method="POST" name="form1" action="./FileUploadServlet" enctype="multipart/form-data" ><% String name= request.getParameter("name");%>File:
<input type="file" name="file" id="file" />
<input type="submit" value="Upload" name="upload" id="upload" />
<a href="./file/<%=name%>"><%=name%></a>
FileUploadServlet.java
// Create path components to save the file
final String docs="C:\\Users\\tushar\\Documents\\NetBeansProjects\\fileupload\\web\\file\\";
final Part filePart = request.getPart("file");
final String file = getFileName(filePart);String a=request.getParameter("id");
OutputStream out = null;
InputStream filecontent = null;
final PrintWriter writer = response.getWriter(); try{
out = new FileOutputStream(new File(docs +""+file));
filecontent = filePart.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = filecontent.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
response.sendRedirect("upload.jsp?id="+a+"&name="+file+"");
// LOGGER.log(Level.SEVERE, "Problems during file upload. Error: {0}", new Object[]{fne.getMessage()});
} finally {
if (out != null) {
out.close();
}
if (filecontent != null) {
filecontent.close();
}
if (writer != null) {
writer.close();
}
答案 0 :(得分:0)
最初,当您加载页面时,没有上传文件,导致名称为空。
您最初可以将名称设置为""
,或者在JSP中进行空检查,其中任何一项都将删除初始值。
您的值正在response.sendRedirect("upload.jsp?id="+a+"&name="+file+"");
中发送,因此请将此处的文件变量设置为值或空字符串以避免空值。