http://blog.csdn.net/xwchen/archive/2007/04/16/1566300.aspx
java程序和接收的servlet如下:
package com.swt.test;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UploadXMLFile {
/**
* 上传xml文件
*
* @param strFile
* 上传带绝对路径的文件名
* @param strURL
* @return result 上传成功,result="SUCCESS",其他则为出错信息。
*/
public synchronized String uploadXMLFile(final String strFile, final String strURL) {
String result = null;
try {
URL url = new URL(strURL + "?filename=" + strFile);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestProperty("enctype", "multipart/form-data");
conn.setRequestProperty("contentType", "charset=GBK");
conn.setRequestMethod("POST");
//上传xml文件
DataInputStream file = new DataInputStream(new FileInputStream(
new File(strFile)));
OutputStream out = conn.getOutputStream();
int bytesOut = 0;
byte[] bufferOut = new byte[8192];
while ((bytesOut = file.read(bufferOut, 0, 8192)) != -1) {
out.write(bufferOut, 0, bytesOut);//
}
out.flush();
out.close();
//获得上传的结果
InputStream isResult = conn.getInputStream();
byte[] buffer = new byte[isResult.available()];
isResult.read(buffer);
result = new String(buffer);
isResult.close();
} catch (Exception e) {
result = "ERROR";
System.out.println(e.getMessage());
}
return result;
}
public static void main(String[] args) {
UploadXMLFile uxf = new UploadXMLFile();
System.out.println("上传文件结果");
String sb = uxf.uploadXMLFile("D:\\swt.xml",
"http://localhost:8080/beh/xml.163");
System.out.println(sb);
}
}
接收的servlet
package com.swt.servlet;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*
* @author sweater
*/
public class GetXMLFile extends HttpServlet {
private final static Log log = LogFactory.getLog(LoginAction.class);
/**
* Constructor of the object.
*/
public GetXMLFile() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(this.getClass());
//System.out.println((String)request.getParameter("filename"));
response.setContentType("text/html");
PrintWriter outResult = null;
try {
outResult = response.getWriter();
} catch (IOException ioE) {
}
String saveFile = "D:\\get.xml";//""";
String uploadReult = "ERROR";
int reqStreamLen = request.getContentLength();
BufferedReader readFile = null;
BufferedWriter writeFile = null;
try {
InputStream is = request.getInputStream();
OutputStream os = new FileOutputStream(saveFile);//建立一个上传文件的输出流"D:\\get.xml"
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);//传过来的文件写入文件
//System.out.println(new String(buffer));
}
os.flush();
os.close();
is.close();
} catch (UnsupportedEncodingException e) {
uploadReult = "UnsupportedEncodingException";
log.info(e.getMessage());
e.printStackTrace();
} catch (FileNotFoundException e) {
uploadReult = "FileNotFoundException";
log.info(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
uploadReult = "IOException";
log.info(e.getMessage());
e.printStackTrace();
}
String uploadFile = (String)request.getParameter("filename");
File file = new File(saveFile);
log.info(String.valueOf(file.length()));
log.info(String.valueOf(reqStreamLen));
if(file.exists()&&(file.length()==reqStreamLen)){
uploadReult = "SUCCESS";
log.info(request.getRemoteAddr() + "上传文件:" + uploadFile + " 成功");
}else{
uploadReult = "ERROR";
log.info(request.getRemoteAddr() + "上传文件:" + uploadFile + " 失败");
}
outResult.println(uploadReult);
outResult.flush();
outResult.close();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
}
}