表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form- data,才能完整的传递文件数据,进行下面的操作.
enctype=\"multipart/form-data\"是上传二进制数据; form里面的input的值以2进制的方式传过去。
form里面的input的值以2进制的方式传过去,所以request就得不到值了。 也就是说加了这段代码,用request就会传递不成功,即不能用request.getParameter()来获取到各个表单元素的值的
例子如下:
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
/**
* java 模拟 ENCTYPE="multipart/form-data"方式
* 提交FORM
* */
public class MultipartPOST {
/**
* @param args
*/
public static void main(String[] args) {
URL url = null;
HttpURLConnection httpurlconnection = null;
try
{
/**
* multipart/form-data 分隔符(boundary)是必须的.
* ----multipartPostRequestMCUServer 是分隔符,分隔多个文件、表单项
* */
url = new URL("http://10.50.8.225:80/mcu");
String boundary = "----multipartPostRequestMCUServer";
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setDoOutput(true);
httpurlconnection.setRequestMethod("POST");
httpurlconnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
/*put the data from the FORM into a Hashtable*/
Hashtable<String,String> hashtable = new Hashtable<String,String>();
hashtable.put("peername", "10");
hashtable.put("operation", "login");
String boundaryMessage = getBoundaryMessage(boundary,hashtable,"","","");
String endBoundary = "\r\n--" + boundary + "--\r\n";
System.out.println( boundaryMessage);
OutputStream dout = httpurlconnection.getOutputStream();
dout.write(boundaryMessage.getBytes());
dout.write(endBoundary.getBytes());
dout.flush();
dout.close();
int code = httpurlconnection.getResponseCode();
System.out.println("-------code " + code);
InputStream is = httpurlconnection.getInputStream();
try {
byte[] buffer = new byte[1024];
@SuppressWarnings("unused")
int len = -1;
while ((len = is.read(buffer)) != -1) {
System.out.println(new String(buffer));
}
} finally {
is.close();
}
}catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
}
@SuppressWarnings("unchecked")
static String getBoundaryMessage(String boundary, Hashtable params, String fileField, String fileName, String fileType)
{
StringBuffer res = new StringBuffer("--").append(boundary).append("\r\n");
Enumeration keys = params.keys();
while(keys.hasMoreElements())
{
String key = (String)keys.nextElement();
String value = (String)params.get(key);
res.append("Content-Disposition: form-data; name=\"").append(key).append("\"\r\n")
.append("\r\n").append(value).append("\r\n")
.append("--").append(boundary).append("\r\n");
}
if(fileField != null && !"".equals(fileField) && fileName != null && !"".equals(fileName) && fileType!=null&&!"".equals(fileType) ){
res.append("Content-Disposition: form-data; name=\"").append(fileField).append("\"; filename=\"").append(fileName).append("\"\r\n")
.append("Content-Type: ").append(fileType).append("\r\n\r\n");
}
return res.toString();
}
}
分享到:
相关推荐
此外,必须在HTML表单元素的`enctype`属性设置为`multipart/form-data`。这个属性指定了表单数据的编码方式,对于文件上传来说是必需的,因为默认的`application/x-www-form-urlencoded`编码方式不支持文件内容的...
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fileToUpload" id="fileToUpload"> <input type="submit" value="上传文件" name="submit"> </form> ``` ...
以JSON编码格式提交表单数据是...大家应该对如何用表单上传一个文件的写法很熟悉,它需要在HTML中form标记上添加 enctype="multipart/form-data" 声明,就是告诉浏览器要按上传文件模式发送表单数据。而JSON格式提交
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="image"> <input type="submit"> </form> ``` 在对应的`upload.php`文件中,使用PHP的`$_FILES`全局变量来...
在表单中设置`enctype="multipart/form-data"`属性,然后在后台的Servlet或JSP中初始化SmartUpload实例,接着调用其方法处理上传的文件。 例如,以下是一个简单的文件上传示例: ```jsp <%@ page import=...
举例如下: $first = 'Hello'; $second = "World"; $full1 = "$first $second"; # 产生 Hello World $full2 = '$first $second';# 产生 $first $second 可以将字符和数字利用运算符号连接起来。字符被转化成数字,...