导航:首页 > IDC知识 > 服务器接收上传的文件

服务器接收上传的文件

发布时间:2020-10-22 09:25:30

1、Java服务器怎么接收手机上传过来的文件。求具体代码,谢谢,如果觉得悬赏太少的话可以说,我再加。急。。

android客户端代码:
public class MainActivity extends Activity
{
private TextView uploadInfo;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

uploadInfo = (TextView) findViewById(R.id.upload_info);

uploadFile();
}

public void uploadFile()
{
//服务器端地址
String url = "http://192.168.0.108:8080/UploadFileServer/upload";
//手机端要上传的文件,首先要保存你手机上存在该文件
String filePath = Environment.getExternalStorageDirectory()
+ "/1/power.apk";

AsyncHttpClient httpClient = new AsyncHttpClient();

RequestParams param = new RequestParams();
try
{
param.put("file", new File(filePath));
param.put("content", "liucanwen");

httpClient.post(url, param, new AsyncHttpResponseHandler()
{
@Override
public void onStart()
{
super.onStart();

uploadInfo.setText("正在上传...");
}

@Override
public void onSuccess(String arg0)
{
super.onSuccess(arg0);

Log.i("ck", "success>" + arg0);

if(arg0.equals("success"))
{
Toast.makeText(MainActivity.this, "上传成功!", 1000).show();
}

uploadInfo.setText(arg0);
}

@Override
public void onFailure(Throwable arg0, String arg1)
{
super.onFailure(arg0, arg1);

uploadInfo.setText("上传失败!");
}
});

} catch (FileNotFoundException e)
{
e.printStackTrace();
Toast.makeText(MainActivity.this, "上传文件不存在!", 1000).show();
}
}
}
服务器端代码:
public class UploadFileServlet extends HttpServlet
{

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

// 创建文件项目工厂对象
DiskFileItemFactory factory = new DiskFileItemFactory();

// 设置文件上传路径
String upload = this.getServletContext().getRealPath("/upload/");
// 获取系统默认的临时文件保存路径,该路径为Tomcat根目录下的temp文件夹
String temp = System.getProperty("java.io.tmpdir");
// 设置缓冲区大小为 5M
factory.setSizeThreshold(1024 * 1024 * 5);
// 设置临时文件夹为temp
factory.setRepository(new File(temp));
// 用工厂实例化上传组件,ServletFileUpload 用来解析文件上传请求
ServletFileUpload servletFileUpload = new ServletFileUpload(factory);

// 解析结果放在List中
try
{
List<FileItem> list = servletFileUpload.parseRequest(request);

for (FileItem item : list)
{
String name = item.getFieldName();
InputStream is = item.getInputStream();

if (name.contains("content"))
{
System.out.println(inputStream2String(is));
} else if(name.contains("file"))
{
try
{
inputStream2File(is, upload + "\\" + item.getName());
} catch (Exception e)
{
e.printStackTrace();
}
}
}

out.write("success");
} catch (FileUploadException e)
{
e.printStackTrace();
out.write("failure");
}

out.flush();
out.close();
}

// 流转化成字符串
public static String inputStream2String(InputStream is) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = -1;
while ((i = is.read()) != -1)
{
baos.write(i);
}
return baos.toString();
}

// 流转化成文件
public static void inputStream2File(InputStream is, String savePath)
throws Exception
{
System.out.println("文件保存路径为:" + savePath);
File file = new File(savePath);
InputStream inputSteam = is;
BufferedInputStream fis = new BufferedInputStream(inputSteam);
FileOutputStream fos = new FileOutputStream(file);
int f;
while ((f = fis.read()) != -1)
{
fos.write(f);
}
fos.flush();
fos.close();
fis.close();
inputSteam.close();

}

}

2、c# web服务器端如何接收post请求实现上传文件

System.Web.HttpContext.Current.Request.Files属性获取Post/Get方法传递的文件

3、求Tomcat服务器里接收文件上传的代码

tom猫服务器啊

4、本地文件上传到Linux服务器的几种方法

有两种方法上传程序到服务器里面。


如果是win系统服务器,那么打开远程桌面,从本地电脑复制文件,到远程桌面服务器里面,粘贴文件,就可以了。


如果有ip地址,ftp账号密码,也可以用 ftp软件上传。


linux服务器的话, 就是直接用ftp软件上传文件了。

5、电脑安装什么软件可以像服务器那样快速接收各个设备上传的文件

在主电脑上设置共享文件夹,其他电脑上的文件都可以在主电脑上打开,使用

6、java服务器端用传输给客户端文件流(fileinputstream),客户端应该如何接收?(socket )

其实这个问题你应该先想一下socket到底是一个什么东西,在网络编程中socket到底用来做什么的。socket:在操作系统内核中它代表网络连接的一个端点(endpoint),在应用程序中它代表一个打开的文件。socket pair唯一确定的一条网络连接,socket pair就是客户端socket和服务端socket的一个组合。也就是客户端ip和port与服务端ip和port的组合。一条网络连接也就是一条通信的信道,tcp连接代表的信道是全双工的。以信道来讲,逻辑上就存在两个管道来代表输出与输入来发送和接收信息。

那么在应用程序上我们就可以拿到这2个管道来完成socket之间的通信。

以你的应用来看输出流fos就代表着图中的红色管道,那么在服务端就应该是这个样的。

Scoket connSock = serverSocket.accept();//connSock代表着服务端的Socket

InputStream is = connSock.getInputStream();//输入流is代表图中的红色管道

OutputStrea os = connSock.getOutputStream();//输出流os代表图中的绿色管道

在服务端有一个字节缓冲区来存放从is读取的字节数据

byte[] buffer = new byte[1024];

然后服务端读取数据来填充这个字节缓冲区

is.read(buffer);//比如这样

然后就根据你的需求来操作这个buffer了。

然后把处理过后的数据通过os发送给客户端。os就是图中的那个绿色管道

os.write(处理过后的数据);

7、java中如何实现从客户端发送文件到服务器端

服务器端源码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
*
* 文件名:ServerReceive.java
* 实现功能:作为服务器接收客户端发送的文件
*
* 具体实现过程:
* 1、建立SocketServer,等待客户端的连接
* 2、当有客户端连接的时候,按照双方的约定,这时要读取一行数据
* 其中保存客户端要发送的文件名和文件大小信息
* 3、根据文件名在本地创建文件,并建立好流通信
* 4、循环接收数据包,将数据包写入文件
* 5、当接收数据的长度等于提前文件发过来的文件长度,即表示文件接收完毕,关闭文件
* 6、文件接收工作结束

public class ServerReceive {

public static void main(String[] args) {

/**与服务器建立连接的通信句柄*/
ServerSocket ss = null;
Socket s = null;

/**定义用于在接收后在本地创建的文件对象和文件输出流对象*/
File file = null;
FileOutputStream fos = null;

/**定义输入流,使用socket的inputStream对数据包进行输入*/
InputStream is = null;

/**定义byte数组来作为数据包的存储数据包*/
byte[] buffer = new byte[4096 * 5];

/**用来接收文件发送请求的字符串*/
String comm = null;

/**建立socekt通信,等待服务器进行连接*/
try {
ss = new ServerSocket(4004);
s = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}

/**读取一行客户端发送过来的约定信息*/
try {
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
comm = br.readLine();
} catch (IOException e) {
System.out.println("服务器与客户端断开连接");
}

/**开始解析客户端发送过来的请求命令*/
int index = comm.indexOf("/#");

/**判断协议是否为发送文件的协议*/
String xieyi = comm.substring(0, index);
if(!xieyi.equals("111")){
System.out.println("服务器收到的协议码不正确");
return;
}

/**解析出文件的名字和大小*/
comm = comm.substring(index + 2);
index = comm.indexOf("/#");
String filename = comm.substring(0, index).trim();
String filesize = comm.substring(index + 2).trim();

/**创建空文件,用来进行接收文件*/
file = new File(filename);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("服务器端创建文件失败");
}
}else{
/**在此也可以询问是否覆盖*/
System.out.println("本路径已存在相同文件,进行覆盖");
}

/**【以上就是客户端代码中写到的服务器的准备部分】*/

/**
* 服务器接收文件的关键代码*/
try {
/**将文件包装到文件输出流对象中*/
fos = new FileOutputStream(file);
long file_size = Long.parseLong(filesize);
is = s.getInputStream();
/**size为每次接收数据包的长度*/
int size = 0;
/**count用来记录已接收到文件的长度*/
long count = 0;

/**使用while循环接收数据包*/
while(count < file_size){
/**从输入流中读取一个数据包*/
size = is.read(buffer);

/**将刚刚读取的数据包写到本地文件中去*/
fos.write(buffer, 0, size);
fos.flush();

/**将已接收到文件的长度+size*/
count += size;
System.out.println("服务器端接收到数据包,大小为" + size);
}

} catch (FileNotFoundException e) {
System.out.println("服务器写文件失败");
} catch (IOException e) {
System.out.println("服务器:客户端断开连接");
}finally{
/**
* 将打开的文件关闭
* 如有需要,也可以在此关闭socket连接
* */
try {
if(fos != null)
fos.close();
} catch (IOException e) {
e.printStackTrace();
}//catch (IOException e)
}//finally

}//public static void main(String[] args)
}//public class ServerReceive

客户端源码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.Socket;

/**
*
* 文件名:ClientSend.java
* 实现功能:作为客户端向服务器发送一个文件
*
* 具体实现过程:
* 1、建立与服务器端的连接,IP:127.0.0.1, port:4004
* 2、将文件的名字和大小通过自定义的文件传输协议,发送到服务器
* 3、循环读取本地文件,将文件打包发送到数据输出流中
* 4、关闭文件,结束传输
*
* */

public class ClientSend {

public static void main(String[] args) {

/**与服务器建立连接的通信句柄*/
Socket s = null;

/**定义文件对象,即为要发送的文件
* 如果使用绝对路径,不要忘记使用'/'和'\'的区别
* 具体区别,请读者自行查询
* */
File sendfile = new File("API.CHM");
/**定义文件输入流,用来打开、读取即将要发送的文件*/
FileInputStream fis = null;
/**定义byte数组来作为数据包的存储数据包*/
byte[] buffer = new byte[4096 * 5];

/**定义输出流,使用socket的outputStream对数据包进行输出*/
OutputStream os = null;

/**检查要发送的文件是否存在*/
if(!sendfile.exists()){
System.out.println("客户端:要发送的文件不存在");
return;
}

/**与服务器建立连接*/
try {
s = new Socket("127.0.0.1", 4004);
}catch (IOException e) {
System.out.println("未连接到服务器");
}

/**用文件对象初始化fis对象
* 以便于可以提取出文件的大小
* */
try {
fis = new FileInputStream(sendfile);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}

/**首先先向服务器发送关于文件的信息,以便于服务器进行接收的相关准备工作
* 具体的准备工作,请查看服务器代码。
*
* 发送的内容包括:发送文件协议码(此处为111)/#文件名(带后缀名)/#文件大小
* */
try {
PrintStream ps = new PrintStream(s.getOutputStream());
ps.println("111/#" + sendfile.getName() + "/#" + fis.available());
ps.flush();
} catch (IOException e) {
System.out.println("服务器连接中断");
}

/**
* 此处睡眠2s,等待服务器把相关的工作准备好
* 也是为了保证网络的延迟
* 读者可自行选择添加此代码
* */
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}

/**之前的准备工作结束之后
* 下面就是文件传输的关键代码
* */
try {

/**获取socket的OutputStream,以便向其中写入数据包*/
os = s.getOutputStream();

/** size 用来记录每次读取文件的大小*/
int size = 0;

/**使用while循环读取文件,直到文件读取结束*/
while((size = fis.read(buffer)) != -1){
System.out.println("客户端发送数据包,大小为" + size);
/**向输出流中写入刚刚读到的数据包*/
os.write(buffer, 0, size);
/**刷新一下*/
os.flush();
}
} catch (FileNotFoundException e) {
System.out.println("客户端读取文件出错");
} catch (IOException e) {
System.out.println("客户端输出文件出错");
}finally{

/**
* 将打开的文件关闭
* 如有需要,也可以在此关闭socket连接
* */
try {
if(fis != null)
fis.close();
} catch (IOException e) {
System.out.println("客户端文件关闭出错");
}//catch (IOException e)
}//finally

}//public static void main(String[] args)
}//public class ClientSend

8、怎么从本地往windows服务器上传文件

1、使用rz和sz命令:rz是从linux端接收windows数据,sz是linux端发送数据到windows端;rz和sz使用ZModem协议进行文件传输。有点是操作简单。2、在windows下使用winscp进行数据上传。winscp是一个Windows环境下支持SFTP, SCP 以及 FTP 的开源图形化客户端。它可以完成本地与linux远程主机间安全的复制文件。有点是可视化,多文件操作,绿色开源。

9、如何向linux服务器上传文件

使用rz和sz命令:rz是从linux端接收windows数据,sz是linux端发送数据到windows端;rz和sz使用ZModem协议进行文件传输。

10、服务器是 如何 接受 浏览器上传的文件信息的?请帮忙解答一下。

架ftp服务器呀,
如果你是说网页上传的话可以上网搜索一下“asp上传文件源码”或者“php上传文件源码”

与服务器接收上传的文件相关的知识