通过HTTP POST请求发送多部分表单数据

时间:2016-03-18 03:06:35

标签: java forms jsp file-upload

我有一个关于使用多部分编码上传文件的问题。我创建了以下jsps。

upload.jsp包含将上传输入文件的表单 uploadAction.jsp包含提取数据并将其保存在服务器中的代码。

即使我指定了enctype =" multipart / form-data"在表单标记中,它在uploadAction.jsp中将内容类型显示为application / x-www-form-urlencoded 有人可以解释代码有什么问题,以及为什么内容类型在uploadAction.jsp中保留为multipart / form-data

upload.jsp

 <script>
function try1(){
        document.forms[0].action = "uploadAction.jsp";
        document.forms[0].submit();
    }
</script>

 <form id="attachmentUploadFormId1" enctype="multipart/form-data" method="POST">
                    <input type='file' id='attachmentFileNameId1' name='attachmentFileNam1e' size='50'>
                    <input class="inforFormButton" type='submit' id="attachmentUploadSumitId1" name="Attach" value="Attach" onclick="try1()" >
</form> 

uploadAction.jsp

<html>
<head>
<title>File Upload Result</title>
</head>
<body>
 <br>
 <h2>File Upload Results</h2>
<br>
<%@ page import="java.io.*,java.util.*" %>

<%
String contentType = request.getContentType();
System.out.println("Content type is :: " +contentType);
if (contentType != null) {

DataInputStream in = new DataInputStream(request.getInputStream());
System.out.println("stream: " + request.getInputStream());

int formDataLength = request.getContentLength();
System.out.println("content length:" + Integer.toString(formDataLength));
/***rest of the code to process content****/
uploadAction.jsp中的

request.getContentType()导致application / x-www-form-urlencoded而不是multipart / form-data的值。请告诉我为什么不保留内容类型。

0 个答案:

没有答案