求上传文件代码,swing方面的 要求从本地选择文件然后上传到服务器即可,用一个类实现. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果服务器有FTP服务,你就做一个FTP的客户端即可如果服务器有JSP服务,你可以用jspSmartUpload组件如果什么服务都没有,你需要用Socket通讯来自己实现,写一个服务器端程序来接收上传的文件和一个发送文件的客户端. String exsistingFileName = (String) file_path.getValue(); try { String g = Global.getServerUrl(); g = g.substring(g.indexOf("=") + 1, g.lastIndexOf("/") - 5);// 去掉 gfmis FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName));//exsistingFileName打包后的文件名 URL url = new URL(g); //urlString是网页路径,这个路径负责解压后的文件处理。 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type","multipart/form-data;boundary="); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); // dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"upload\";" + " filename=\"" + exsistingFileName + "\""); // dos.writeBytes(lineEnd); // create a buffer of maximum size int maxBufferSize = 1024; int bytesAvailable = fileInputStream.available(); int bufferSize = Math.min(bytesAvailable, maxBufferSize); byte[] buffer = new byte[bufferSize]; int bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0) { dos.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } // dos.writeBytes(lineEnd); // dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); fileInputStream.close(); dos.flush(); dos.close(); } catch (MalformedURLException ex) { } catch (IOException ioe) { } 刚抄袭的一个,不过应该还有一部分 刚找到第一份工作.请大家为我指明方向 关于openfire接收HTTP请求的问题 一个简单的问题,希望答案详尽一些~~ java如何实现多态 小菜鸟,急问字符串问题请教,谢谢 JTable高手求救,我想高手毕竟是善良的 Data的问题 今天面试题:从客户端输入一字符串“localhost”作为服务器ip连接至服务器, 哪位大侠对xalan熟 真的很急!急!急!急急急急急急! 能不能帮忙系统的讲解一下java富客户端的文件上传下载(ftp,http) 这个数组如何进行降序排序呢??????????
如果服务器有JSP服务,你可以用jspSmartUpload组件
如果什么服务都没有,你需要用Socket通讯来自己实现,写一个服务器端程序来接收上传的文件和一个发送文件的客户端.
try
{
String g = Global.getServerUrl();
g = g.substring(g.indexOf("=") + 1, g.lastIndexOf("/") - 5);// 去掉 gfmis
FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName));//exsistingFileName打包后的文件名
URL url = new URL(g); //urlString是网页路径,这个路径负责解压后的文件处理。
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=");
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
// dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"upload\";" + " filename=\"" + exsistingFileName + "\"");
// dos.writeBytes(lineEnd); // create a buffer of maximum size
int maxBufferSize = 1024;
int bytesAvailable = fileInputStream.available();
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize]; int bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
} // dos.writeBytes(lineEnd);
// dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); fileInputStream.close();
dos.flush();
dos.close(); }
catch (MalformedURLException ex)
{
}
catch (IOException ioe)
{
}
刚抄袭的一个,不过应该还有一部分