java源码
<pre name="code" class="java">import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import sun.net.www.protocol.http.HttpURLConnection;
/**
* 主要实现文件上传,和接收方绑定接收后信息导入参数传递
* @author 张勇波
*
*/
public class HttpURLConnectionServices {
/**
* @param fileName 要上传的文件,列:e:/upload/SSD4k对齐分区.zip
* @param Url 上传路径端口号和项目名称,列:http://192.168.1.209:9080/gjbmj
* @param strSiteID 对方的站点编号
* @param strColumnID 对方的栏目编号
* @param strDespatcher 发送信息人
* @param strMechanism 发送信息机构
* @param strOther1
*/
public static void post(String fileName ,String Url,String strSiteID,String strColumnID,String strDespatcher,String strMechanism,String strOther1){
try {
String fname =fileName;//要上传的文件
File file = new File(fname);
URL url = new URL(Url+"/cms/infoShare/httpURLConnection.jsp");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setChunkedStreamingMode(1024 * 1024);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setConnectTimeout(50000);
conn.setRequestProperty("Content-Type", "multipart/form-data;file="+ file.getName());
conn.setRequestProperty("fileName",file.getName());
conn.setRequestProperty("strSiteID", strSiteID);
conn.setRequestProperty("strColumnID", strColumnID);
conn.setRequestProperty("strDespatcher", strDespatcher);
conn.setRequestProperty("strMechanism", strMechanism);
conn.setRequestProperty("strOther1", strOther1);
conn.connect();
OutputStream out = new DataOutputStream(conn.getOutputStream());
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[2048];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
//System.out.println("---line---"+line);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
new File(fileName).delete();
}
}
}
jsp接收
<pre name="code" class="html"><%@page import="com.fsm.db.DBSet"%>
<%@page import="com.fsm.db.DBService"%>
<%@page import="com.fsm.config.ConfigInfo"%>
<%@page import="com.fsm.cms.imexport.ImexportServices"%>
<%@page import="java.io.*;"%>
<%
DBService dbservice = new DBService();
DBSet dbset = new DBSet();
String strOther1 =new String(request.getHeader("strOther1").getBytes("iso-8859-1"), "GBK");
String strSiteID =new String(request.getHeader("strSiteID").getBytes("iso-8859-1"), "GBK");
String strColumnID =new String(request.getHeader("strColumnID").getBytes("iso-8859-1"), "GBK");
String strSQL="SELECT * FROM P_INFOSHARECONFIG WHERE KID='"+strOther1+"' and Tstate='start'";
String SiteID="",ColumnID="";
dbset = dbservice.getSelectSet(strSQL);
if (dbset != null && dbset.size() > 0)
{
for (int i = 0; i < dbset.size(); i++) {
SiteID = dbset.getValue(i, "SiteID").trim();
ColumnID = dbset.getValue(i, "ColumnID").trim();
}
if(SiteID.indexOf(strSiteID)!=-1 && ColumnID.indexOf(strColumnID)!=-1){
String name =new String(request.getHeader("fileName").getBytes("iso-8859-1"), "GBK");
String strDespatcher =new String(request.getHeader("strDespatcher").getBytes("iso-8859-1"), "GBK");
String strMechanism =new String(request.getHeader("strMechanism").getBytes("iso-8859-1"), "GBK");
InputStream in = request.getInputStream();
ImexportServices ips=new ImexportServices();
ConfigInfo configinfo = new ConfigInfo();
String strPath = configinfo.getPropertiesValue("parameter", "workpath")+ "imp/";
//out.print("---path---"+strPath+name+"---name---"+name.substring(0,name.lastIndexOf(".")));
File file = new File(strPath+name);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream fos = new FileOutputStream(file);
byte[] buffer = new byte[2048];
int bytes = 0;
while ((bytes = in.read(buffer)) != -1) {
fos.write(buffer, 0, bytes);
}
fos.flush();
fos.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
//out.print("-----strSiteID----"+strSiteID+"---strColumnID---"+strColumnID+"----name----"+name.substring(0,name.lastIndexOf("."))+"--path--"+request.getContextPath().trim());
//接收文件后调用导入信息
ips.impInfo(strSiteID, strColumnID, name.substring(0,name.lastIndexOf(".")), request.getContextPath().trim(),strDespatcher,strMechanism,strOther1);
}
}
%>