0

I am uploading a file and displaying it as a link...but initially when I load the the page it displays a null value...I want remove this null value...

<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();
    }
4

1 回答 1

0

最初,当您加载页面时,没有上传文件,导致名称为空。

您可以将名称设置为""初始名称或在 JSP 中进行空值检查,其中任何一个都将删除初始空值。

您的值在response.sendRedirect("upload.jsp?id="+a+"&name="+file+"");此发送,因此将此处的文件变量设置为一个值或空字符串以避免空值。

于 2013-03-11T04:58:31.097 回答