0

ファイルをアップロードしてリンクとして表示しています...しかし、最初にページをロードするとnull値が表示されます...このnull値を削除したい...

<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

最初にページをロードしたとき、ファイルがアップロードされていないため、名前が null になります。

名前を""初期値として設定するか、JSP で null チェックを行うことができます。どちらの方法でも初期 null が削除されます。

値はresponse.sendRedirect("upload.jsp?id="+a+"&name="+file+"");これで送信されるため、ここでファイル変数を値または空の文字列に設定して、null 値を回避します。

于 2013-03-11T04:58:31.097 に答える