关于上传 将指定的文件(如:test.db)上传至我的空间中(比如我的空间地址是:http://xxx.com/abc),如何通过j2se实现。j2se数据上传上传上传服务器上传空间 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 具体怎么实现呢,就类似于像QQ似的将照片上传到它自己的空间里一样,我是要将一个数据库文件(test.db)上传到我自己的空间里。 我写了一份代码,执行没出任何错误,可是空间里却没有所要传的那份文件,还请高人指教啊。public static void upload() throws Exception { 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); } } http://blog.csdn.net/xu_201205/article/details/7972553 看下吧 你这也太牛了吧随便给一个地址就能上传文件有这功能足以秒杀一切黑客了你最起码在服务器端设置一下,比如服务器端有个ftp,或者有其他程序 +1服务器段弄个ftp,写java通过ftp上传的代码 现在我弄了一个FTP,并且在Eclipse测试向FTP的指定目录下上传文件没有问题,可是打包后,运行,却连不上FTP,发给朋友测试也是连不上,这是为什么呀,在Eclipse下就没问题 java添加音乐 Java Swing的日历控件谁有啊! javaAPI 创建隐藏文件夹 帮我解释一下这段程序(红色的是什么意思) 问下关于SESSION的东西 处女贴!想知道关于Java多态的详细解说!从生命起源说起也可以!万分谢谢 如何调整TextField的高度? 数据库问题 急!!大家看看我写的程序错哪里了? replaceAll,请问如何忽略大小写替换??? 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上传的代码