문제

I successfully uploaded a text file (say Another.java) using the following code, but it gives me an error while trying to open the uploaded file. Thanks in advance.

fileUpload.jsp

<form action="test.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<br/>
<input type="submit" value="Upload">
</form>
</body>

test.jsp

<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="java.io.*"%>
<%@page import="java.util.*"%>
<%-- <%@page import="org.apache.commons.io.*" %> --%>
<%@page import="org.apache.commons.io.*"%>

<%@page import="org.apache.commons.fileupload.FileUploadException"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%
    try {
        String username = "";
        List<FileItem> items = new ServletFileUpload(
                new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items) {
            if (item.isFormField()) {
                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                String fieldname = item.getFieldName();
                String fieldvalue = item.getString();

                if (fieldname.equals("vsrd")) {
                    username = fieldvalue;
                }

                // ... (do your job here)
            } else {
                // Process form file field (input type="file").
                String fieldname = item.getFieldName();

                String filename = FilenameUtils.getName(item.getName());

                InputStream filecontent = item.getInputStream();

                byte[] b = new byte[filecontent.available()];
                FileOutputStream fos = new FileOutputStream(
                        "/home/visruth/Desktop/Out" + filename);
                fos.write(b);
                fos.close();
                // ... (do your job here)
            }
        }
    } catch (FileUploadException e) {
        throw new ServletException("Cannot parse multipart request.", e);
    }
%>

error while trying to open OutAnother.java:

enter image description here

도움이 되었습니까?

해결책 2

I fixed it by adding the following code :

byte[] b = new byte[filecontent.available()];
filecontent.read(b);

다른 팁

In your code, you just created an empty byte array. You have NOT read content from InputStream. Inputstream#available() just get available length. It won't read content from InputStream.

Typical way is to read from InputStream and write to OutputStream:

FileInputStream is = new FileInputStream(
        new File("D:\\temp\\in.java"));
FileOutputStream os = new FileOutputStream(
        new File("D:\\temp\\out.java"));
byte[] buff = new byte[1000];
int length = -1;
while ((length = is.read(buff)) != -1) {
    os.write(buff, 0, length);
}
is.close();
os.close();

You can also use IOUtils#copy from commons-io to do this job.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top