将指定的文件(如:test.db)上传至我的空间中(比如我的空间地址是:http://xxx.com/abc),如何通过j2se实现。j2se数据上传上传上传服务器上传空间
解决方案 »
- poi excel 格式问题
- 我想问下,(在solaris下)如何使用java程序调出dos窗口来实现ping ip的功能?(注意,要弹出dos窗口显示ping信息)。谢谢。
- javac java的郁闷
- 完全数
- import javax.mail.*;提示找不到,是否需要给jdk添加额外的包?
- JAVA调用存储过程的问题
- 求救:页面上的图形为什么不能及时刷新?急!
- 用ObjectInputStream从文件读全部的学生类(只有学生类)。求指点
- 调用什么函数可以将文本框的内容清空?
- 请问有谁用过itext,下面这个错误是什么引起(将html转化为pdf时)?
- openoffice3.2 sdk linux安装和配置
- apache-common-io的文件监听问题
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null; String exsistingFileName = "D:\\scrdm.db";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String urlString = "http://longbq.com/photo/"; try {
// ------------------ CLIENT REQUEST
FileInputStream fileInputStream = new FileInputStream(new File( exsistingFileName));
// open a URL connection to the Servlet
URL url = new URL(urlString);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
// dos = new DataOutputStream(System.out);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadfile\";" + " filename=\"" + exsistingFileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
System.out.println(new String(buffer));
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// close streams
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
System.out.println("From ServletCom CLIENT REQUEST:" + ex);
} catch (IOException ioe) {
System.out.println("From ServletCom CLIENT REQUEST:" + ioe);
}
// ------------------ read the SERVER RESPONSE
try {
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(in); int readLength = 0; byte[] toReadByte = new byte[200]; while ((readLength = bis.read(toReadByte, 0, 200)) != -1) {
byte[] readByte = new byte[readLength]; System.arraycopy(toReadByte, 0, readByte, 0, readLength);
System.out.println(new String(readByte, "UTF-8"));
} in.close();
}
} catch (IOException ioex) {
System.out.println("From (ServerResponse): " + ioex);
}
}
随便给一个地址就能上传文件
有这功能足以秒杀一切黑客了你最起码在服务器端设置一下,比如服务器端有个ftp,或者有其他程序
+1服务器段弄个ftp,写java通过ftp上传的代码