日期:2014-05-16  浏览次数:20316 次

JSP上传文件时无法上传别的表单数据问题所在
JSP上传文件时无法上传别的表单数据问题所在

在JSP中上传文件,图片的时候,我们都会在form里设置属性enctype="multipart/form-data"。这时,我们在后台servlet中使用request.getParameter()时,取不出传过来的 数据。这是因为使用了enctype="multipart/form-data"后,就说明,你要上传数据的方式是以流的形式传递,当然在servlet中就没法使用request.getParamete()了。

下面是测试的代码:
servlet的:

package org.hwq.upload;

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UploadFileAction extends HttpServlet{

	public void service(HttpServletRequest request, HttpServletResponse response){
		try {
			String path = request.getParameter("filename");
			System.out.println(path);
			ServletInputStream in = request.getInputStream();
			File desfile = new File("d:\\upload.txt");
			OutputStream out = new FileOutputStream(desfile);
			byte[] buf = new byte[1024*5];
			int length = 0;
			while((length=in.read(buf))!=-1){
				out.write(buf);
			}
			out.flush();
			out.close();
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



前台1:
<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!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=GBK">
<title>upload file demo</title>
</head>
<body>
	<form id="form" action="/uploadfile/upload" method="post" enctype="multipart/form-data">
		<input type="file" name="uploadfile"/><br>
		<input type="hidden" name="filename" value="hello2fsdfsdfsdfdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff">
		<input type="submit" value="提交"/>
		<input type="reset" value="重置"/>
	</form>
</body>
</html>


这时候后来输出了“null”,说明request.getParameter("filename")为空,没取出数据。再来看看上传上来的文件打开是什么呢?

-----------------------------7dcfa33d0c2e
Content-Disposition: form-data; name="uploadfile"; filename="C:\Documents and Settings\Administrator\桌面\upload.txt"
Content-Type: text/plain


-----------------------------7dcfa33d0c2e
Content-Disposition: form-data; name="filename"

hello2fsdfsdfsdfdffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
-----------------------------7dcfa33d0c2e--




在改下前台JSP页面信息:

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<!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=GBK">
<title>upload file demo</title>
</head>
<body>
	<form id="form" action="/uploadfile/upload" method="post" enctype="multipart/form-data">
		<input type="file" name="uploadfile"/><br>
		<input type="submit" value="提交"/>
		<input type="reset" value="重置"/>
	</form>
</body>
</html>


把那行hidden删掉了,我们在看看结果会是怎么样的呢。

后台控制台还是输出null,上传文件如下:

-----------------------------7dc37a21d0c2e
Content-Disposition: form-data; name="uploadfile"; filename="C:\Documents and Settings\Administrator\桌面\upload.txt"
Content-Type: text/plain


-----------------------------7dc37a21d0c2e--



上面我上传的是同一个空文件“upload.t