日期:2014-05-16 浏览次数:20387 次
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();
}
}
}
<%@ 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>-----------------------------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--
<%@ 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>-----------------------------7dc37a21d0c2e Content-Disposition: form-data; name="uploadfile"; filename="C:\Documents and Settings\Administrator\桌面\upload.txt" Content-Type: text/plain -----------------------------7dc37a21d0c2e--