httpurlconnection发送文件到服务端并接收
2024-08-26 01:49:32
httpurlconnection发送文件到服务端并接收
客户端
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/*
* @author xk
* 上传文件到文件服务器的客户端
*/
public class FileUploadClient{
public static void main(String[] args) {
try {
// URL url = new URL("http://127.0.0.1:8888/fileSys/getFile.action?filePath=/c/v/b/&fileName=test.zip");
URL url = new URL("http://127.0.0.1:8888/fileSys/getFile.action?filePath=/c/v/b/&fileName=test.zip");
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
// 发送POST请求必须设置如下两行 conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type","text/html");
conn.setRequestProperty("Cache-Control","no-cache");
conn.setRequestProperty("Charsert", "UTF-8");
conn.connect();
conn.setConnectTimeout();
OutputStream out =conn.getOutputStream(); File file = new File("C:\\fileSys\\test.zip"); DataInputStream in = new DataInputStream(new FileInputStream(file)); int bytes = ;
byte[] buffer = new byte[];
while ((bytes = in.read(buffer)) != -) {
out.write(buffer, , bytes);
}
in.close();
out.flush();
out.close(); conn.getInputStream();
conn.disconnect();
} catch (Exception e) {
System.out.println("发送文件出现异常!" + e);
e.printStackTrace();
}
}
}
服务端
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.property.PropertyUtil; public class GetFileAction extends ActionSupport { private static final long serialVersionUID = -9108080625441081032L;
public String execute() throws Exception
{
String rootPath = PropertyUtil.getProperty("fileSys.properties","fileSys.path"); HttpServletRequest request = ServletActionContext.getRequest();
String filePath = request.getParameter("filePath");
String fileName = request.getParameter("fileName"); InputStream input = request.getInputStream();
String fileFullPath = rootPath + filePath + fileName; File file = new File(rootPath + filePath);
if(!file.exists()){
file.mkdirs();
}
FileOutputStream fos = new FileOutputStream(fileFullPath); int size = ;
byte[] buffer = new byte[];
while ((size = input.read(buffer,,)) != -) {
fos.write(buffer, , size);
}
fos.close();
input.close(); return Action.NONE;
}
}
如果用jersey,形成RESTfull webservices 就更有用了
最新文章
- dev中控件属性设置
- javascript 之注意url特殊字符限制
- js 实现list类
- position绝对剧中
- 【Python开发实战】Python环境的配置
- JAVA程序生成XML标准化的文件格式,缩进,美化。
- Python - 字母算术谜题
- 专业辟谣----ThinkSNS不仅仅是微博程序!
- [Android]上传到多个Maven仓库的Gradle插件RapidMavenPushPlugin
- linux TCP数据包封装在SKB的过程分析
- 动态将ASPX生成HTML网页并将网页导出PDF
- 安装部署jumpserver3.0
- 用php实现斐波那契数列,如: 1, 1, 2, 3, 5, 8, 13, 21, 34。求出第20个数的值。
- NET 使用 RabbitMQ
- Python经典算法片段
- python 多线程和多进程
- Redis缓存使用技巧
- OpenGL ES 2.0 Shader 调试新思路(一): 改变提问方式
- java中获取文件目录
- sql语句 case when then else end 语句实例