Servlet图像上传 - 无法获取其他表单字段

时间:2014-04-25 00:38:44

标签: java servlets file-upload

我的图片上传代码出现问题。当我尝试从表单中使用request.getParameter("title")时,上传不会成功。但是当我删除request.getParameter("title")时,它就可以了。

简而言之,我如何访问其他表单字段?我想将它们存储在某个地方。

这是我的表格:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Upload</title>
</head>
<body>
    <div>
        <form action="ImageUpload" method="POST" enctype="multipart/form-data"><br>
            Title:<input type="text" name="title" id="title" /><br>
            Reporter:<input type="text" name="reporter" id="reporter"><br>
            Image:<input type="file" name="image" id="image" /><br>
            <input type="submit" value="Upload">
        </form>
    </div>
</body>
</html>

上传Servlet:

package socialnewsreloaded.upload;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.FileItemIterator;
import org.apache.tomcat.util.http.fileupload.FileItemStream;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.servlet.*;

/**
 * Servlet implementation class ImageUpload
 */
@WebServlet("/ImageUpload")
@MultipartConfig
public class ImageUpload extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);
        String title = request.getParameter("title");
        response.getWriter().print(title);


        if (isMultiPart) {
            ServletFileUpload upload = new ServletFileUpload();

            try {
                FileItemIterator iterate = upload.getItemIterator(request);

                while (iterate.hasNext()) {
                    FileItemStream item = iterate.next();

                    if (item.isFormField()) {
                        String fieldName = item.getFieldName();
                        InputStream inStream = item.openStream();
                        byte[] b = new byte[inStream.available()];
                        inStream.read(b);
                        String value = new String(b);

                        //response.getWriter().print(fieldName+":"+value);  
                    } else {
                        String path = "C:/uploads";

                        if (FileUpload.processFile(path, item)) {

                            response.getWriter().print(
                                    "File Uploaded Successfully");
                        } else {

                            response.getWriter().print(
                                    "Error Uploading File !!");
                        }
                    }
                }
            } catch (FileUploadException e) {
                e.printStackTrace();
            }

        }
    }

}

FileUpload类:

package socialnewsreloaded.upload;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tomcat.util.http.fileupload.FileItemStream;

public class FileUpload {
    public static boolean processFile(String path, FileItemStream stream)
            throws IOException {
        File file = new File(path + File.separator + "images");

        if (!file.exists()) {
            file.mkdir();
        }

        File savedFile = new File(file.getAbsolutePath() + File.separator
                + stream.getName());
        try {
            FileOutputStream outStream = new FileOutputStream(savedFile);
            InputStream inStream = stream.openStream();
            int i = 0;
            byte[] b = new byte[1024];

            while((i=inStream.read(b))!=-1){
                outStream.write(b, 0, i);

            }
            outStream.flush();
            outStream.close();

            return true;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }
}

1 个答案:

答案 0 :(得分:0)

这是打印,而不是getParameter("title").在消耗完所有输入之前,你不应该产生任何输出。