日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java怎么上传文件到web服务器_Java客户端通过Http发送POST请求上传文件到web服务器...

發布時間:2024/9/30 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java怎么上传文件到web服务器_Java客户端通过Http发送POST请求上传文件到web服务器... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://www.cnblogs.com/WilliamJiang/archive/2012/04/29/2475883.html

1.朋友的一個需求,讓我給他實現,需求是這樣的,需要用ASP.net寫一個頁面負責處理客戶端上傳的文件,并根據傳遞的參數把文件保存到相應的目錄??蛻舳耸鞘謾C應用程序,因為沒學過Android,所以我只是寫了一個Java的Demo用來上傳文件。

服務端:

public partial class _Default : System.Web.UI.Page

{

private string id = "";

private string userName = "";

private string type = "";

private string fileName = "";

//文件長度

private long contentLength = 0;

private static readonly string filePath = ConfigurationManager.AppSettings["filePath"];

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

id = Request["id"];

userName = Request["user"];

type = Request["type"];

fileName = Request.Headers["FileName"];

writeFile();

}

}

///

/// 上傳文件

///

private void writeFile()

{

try

{

Stream stream = Request.InputStream;

contentLength = stream.Length;

string currentFilePath = filePath + userName;

if (!Directory.Exists(currentFilePath))

{

Directory.CreateDirectory(currentFilePath);

}

FileStream fileStream = File.Create(currentFilePath + @"\" + fileName);

//每次讀取的1024個字節

byte[] bytes = new byte[1024];

int numReadByte = 0;

while ((numReadByte = stream.Read(bytes, 0, 1024)) != 0)

{

fileStream.Write(bytes, 0,numReadByte);

}

//關閉流

stream.Close();

fileStream.Close();

}

Java文件上傳客戶端示例,(幾年沒搞java有點生疏了):

import java.io.BufferedOutputStream;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.net.HttpURLConnection;

import java.net.URL;

/**

*

* 只是寫的一個示例,filePath,和FileName根據需要進行調整。

*/

public class MyTest {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String str="http://localhost:2906/Default.aspx?id=1&user=2&type=3";

String filePath="D:\\Wildlife.wmv";

String fileName="Wildlife.wmv";

try {

URL url=new URL(str);

HttpURLConnection connection=(HttpURLConnection)url.openConnection();

connection.setDoInput(true);

connection.setDoOutput(true);

connection.setRequestMethod("POST");

connection.addRequestProperty("FileName", fileName);

connection.setRequestProperty("content-type", "text/html");

BufferedOutputStream out=new BufferedOutputStream(connection.getOutputStream());

//讀取文件上傳到服務器

File file=new File(filePath);

FileInputStream fileInputStream=new FileInputStream(file);

byte[]bytes=new byte[1024];

int numReadByte=0;

while((numReadByte=fileInputStream.read(bytes,0,1024))>0)

{

out.write(bytes, 0, numReadByte);

}

out.flush();

fileInputStream.close();

//讀取URLConnection的響應

DataInputStream in=new DataInputStream(connection.getInputStream());

} catch (Exception e) {

e.printStackTrace();

}

}

}

總結

以上是生活随笔為你收集整理的java怎么上传文件到web服务器_Java客户端通过Http发送POST请求上传文件到web服务器...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。