端到后端"/>
java代码实现文件上传到linux服务器,从前端到后端
简单快捷,亲测有效
这种方法可以有效避免这个报错:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3
先附截图:
本地文件:
上传后的远程文件:
**
代码
**
前端:采用了批量上传的模式
<form action="http://localhost:8787/fms-base-service/dc/moreFileUpload" method="post" name="f_upload" enctype="multipart/form-data"><input type="text" name="username" /><br><input type="file" name="filename" multiple="multiple" /><br><input type="submit" value="上传" />
</form>
后台controller:
@RequestMapping("/dc/moreFileUpload")public ResultVo testUpload(MultipartHttpServletRequest request) throws Exception {Iterator<String> fileNames = request.getFileNames();while (fileNames.hasNext()) {//把fileNames集合中的值打出来String fileName=fileNames.next();List<MultipartFile> fileList=request.getFiles(fileName);if (fileList.size()>0) {//遍历文件列表Iterator<MultipartFile> fileIte=fileList.iterator();while (fileIte.hasNext()) {//获得每一个文件MultipartFile file =fileIte.next();/*上传*/String filePath = ""; //映射的地址InputStream input = file.getInputStream();String oldName = file.getOriginalFilename();//获取file图片名称String newName = IdUtils.genImageName();boolean result = FtpUtil.uploadFile("xxxxx", 22, "root", "xx", "/root/file", filePath, newName, input);}}}return ResultMsgHandler.generateSucessMsg(localeMessageSourceService.getMessage(BaseConstants.SAVE_SUCCESS), request);}
工具类:
package com.sinotrans.fms.nvocc.utils;import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.apache.commons.net.ftp.*;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.Properties;public class FtpU {private static ChannelSftp sftp = null;/*** Description: 向FTP服务器上传文件* @param host FTP服务器ip* @param port FTP服务器端口* @param username FTP登录账号* @param password FTP登录密码* @param basePath FTP服务器基础目录,/home/ftpuser/images* @param filePath FTP服务器文件存放路径。例如分日期存放:/2018/05/28。文件的路径为basePath+filePath* @param filename 上传到FTP服务器上的文件名* @param input 输入流* @return 成功返回true,否则返回false*/public static boolean uploadFile(String host, int port, String username, String password, String basePath,String filePath, String filename, InputStream input) {boolean result = false;FTPClient ftp = new FTPClient();File file=null;try {JSch jsch = new JSch();//获取sshSession 账号-ip-端口Session sshSession = jsch.getSession(username, host, port);//添加密码sshSession.setPassword(password);Properties sshConfig = new Properties();//严格主机密钥检查sshConfig.put("StrictHostKeyChecking", "no");sshSession.setConfig(sshConfig);//开启sshSession链接sshSession.connect();//获取sftp通道Channel channel = sshSession.openChannel("sftp");//开启channel.connect();sftp = (ChannelSftp) channel;file=new File(basePath);//设置为被动模式ftp.enterLocalPassiveMode();//设置上传文件的类型为二进制类型//进入到要上传的目录 然后上传文件sftp.cd(basePath);sftp.put(input,filename);input.close();result = true;} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {if (ftp.isConnected()) {try {ftp.disconnect();} catch (IOException ioe) {}}}return result;}
}
生成UUID的工具类:
public class IdUtils {public static String genImageName() {String uuid = UUID.randomUUID().toString().trim();return uuid;}public static void main(String[] args) {String s = genImageName();System.out.println(s);}}
关于文件上传后临时目录清空的问题详见:.html
更多推荐
java代码实现文件上传到linux服务器,从前端到后端
发布评论