本节目标
一、微信小程序端上传文件:wx.uploadFile的使用
二、上传文件的两种保存方式:
1、保存到后端服务器本地
2、保存到文件服务器
详细内容
一、微信小程序端上传文件:wx.uploadFile的使用
语法:
wx.chooseImage({
success (res) {
const tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'https://example.weixin.qq.com/upload', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData: {
'user': 'test'
},
success (res){
const data = res.data
//do something
}
})
}
})
二、上传文件的两种保存形式
1、保存到后端的本地服务器
(1)核心对象:MultipartFile
public interface MultipartFile extends InputStreamSource {
String getName();
// 得到原文件名
@Nullable
String getOriginalFilename();
// 得到头部信息
@Nullable
String getContentType();
//判断是否为空
boolean isEmpty();
//获得文件的大小
long getSize();
//将文件转换成二进制文件
byte[] getBytes() throws IOException;
//将文件转换成输出流
InputStream getInputStream() throws IOException;
//将文件对象封装成Resource对象的形式
default Resource getResource() {
return new MultipartFileResource(this);
}
//将文件写入到新文件中去
void transferTo(File var1) throws IOException, IllegalStateException;
//将文件写入到路径中去
default void transferTo(Path dest) throws IOException, IllegalStateException {
FileCopyUtils.copy(this.getInputStream(), Files.newOutputStream(dest));
}
}
(2)实现步骤(简而言之)
public String upload(@RequestParam("file")MultipartFile file){
File destNew=new File(path+"/文件名.后缀名");
try {
file.transferTo(destNew);
return "success!";
} catch (IOException e) {
e.printStackTrace();
return "fail!";
}
}
2、保存到文件服务器
(1)准备工作
搭建ftp和Nginx服务,配置Nginx配置文件
server {
listen 80;
server_name _;
access_log /data/wwwlogs/access_nginx.log combined;
root /data/wwwroot/default;
index index.html index.htm index.jsp;
#error_page 404 /404.html;
#error_page 502 /502.html;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
location ~ .(jpg|png|jpeg|gif|bmp)$ { #可识别的文件后缀
root /usr/nginx/image/; #图片的映射路径
autoindex on; #开启自动索引
expires 1h; #过期时间
}
location ~ .(css|js)$ {
root /usr/nginx/static/;
autoindex on;
expires 1h;
}
location ~ .(AVI|mov|rmvb|rm|FLV|mp4|3GP)$ {
root /usr/nginx/video/;
autoindex on;
expires 1h;
}
(2)后端java服务器处理步骤
核心对象1:JSch ——java实现服务器远程操作
JSch jSch=new JSch();
Session session= null;
session = jSch.getSession(user,host,port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking","no");
session.connect(CONNECT_TIMEOUT);
核心方法:session.openChannel(String type)----->调用 channel.getChannel(type)
static Channel getChannel(String type) {
if (type.equals("session")) {
return new ChannelSession();
} else if (type.equals("shell")) {
return new ChannelShell();
} else if (type.equals("exec")) {
return new ChannelExec();
} else if (type.equals("x11")) {
return new ChannelX11();
} else if (type.equals("auth-agent@openssh.com")) {
return new ChannelAgentForwarding();
} else if (type.equals("direct-tcpip")) {
return new ChannelDirectTCPIP();
} else if (type.equals("forwarded-tcpip")) {
return new ChannelForwardedTCPIP();
} else if (type.equals("sftp")) {
return new ChannelSftp();
} else {
return type.equals("subsystem") ? new ChannelSubsystem() : null;
}
}
额外示例:ChannelExec对象,远程执行服务命令:mkdir test
String command="mkdir test";
//通过session创建ChannelExec通道对象
ChannelExec channelExec=(ChannelExec) session.openChannel("exec");
channelExec.setCommand(command);
input = channelExec.getInputStream();
//执行连接
channelExec.connect(CONNECT_TIMEOUT);
BufferedReader inputReader=new BufferedReader(new InputStreamReader(input));
String inputLine=null;
while((inputLine=inputReader.readLine())!=null){
System.out.println(inputLine);
}
//退出连接
channelExec.disconnect();
核心对象2:ChannelSftp ——java实现通过SFTP协议,进行文件的上传下载功能
//通过session创建ChannelSftp通道对象
ChannelSftp channelsftp =(ChannelSftp)sshSession.openChannel("sftp");
//上传文件
channelSftp.put(inputStream,"/usr/nginx/image/"+newFileName);
//退出通道
channelSftp.quit();
channelSftp.exit();
源代码下载地址:https://download.csdn.net/download/RitaNBBB/12838721