@Value(("#{prop.uploadHost}"))private String uploadHost;@RequestMapping(value = "/insertImgUrl")public ResultData insertImgUrl(HttpServletRequest request, HttpServletResponse response){ response.setContentType("application/json"); String rootPath = request.getSession().getServletContext().getRealPath("/"); MultipartHttpServletRequest Murequest =(MultipartHttpServletRequest)request; Map<String, MultipartFile> files = Murequest.getFileMap();//得到文件map对象 // 实例化一个jersey Client client = new Client(); for(MultipartFile pic: files.values()){ String originalFilename = pic.getOriginalFilename(); //原来的文件名 //String uploadInfo = Upload.upload(client, pic, request, response, uploadHost, ueditor); String[] txt1 = originalFilename.split("\\."); String txt = txt1[txt1.length - 1]; if (StringUtils.isBlank(originalFilename)) { continue; } File tempFile = new File(ueditor, originalFilename); Long ss = System.currentTimeMillis(); File newfile = new File(tempFile.getParent() + File.separator + ss + "." + txt); tempFile.renameTo(newfile); if (tempFile.exists()) { //newfile = new File(tempFile.getParent() + File.separator + ss+"."+txt); tempFile.renameTo(newfile); } if (!newfile.exists()) { newfile.getParentFile().mkdirs(); try { newfile.createNewFile(); pic.transferTo(newfile); //ImageUtil imageUtil = new ImageUtil(); Integer isUpload=0; SftpUtil sftpUtil=new SftpUtil(); isUpload=sftpUtil.LunboUpload(ss + "." + txt, ss); if(isUpload==0) { return ResultData.returnResultData(ResultData.DATA_FALSE,"上传失败"); } } catch (Exception e) { e.printStackTrace(); } } String uploadInfo=uploadHost+ss+"."+txt; if(!"".equals(uploadInfo)) { return ResultData.returnResultData(ResultData.RESULT_SUCCESS,"ok",uploadInfo); } else { return ResultData.returnResultData(ResultData.DATA_FALSE,"false","上传失败"); } } return ResultData.returnResultData(ResultData.DATA_FALSE,"false","上传失败");}
public synchronized Integer LunboUpload(String fileName,Long s){ ChannelSftp sftp=null; try { SftpUtil sf = new SftpUtil(); String host = ip; int port = 22; String username = root; String password = Password; //String directory = "/data/sftp/mysftp/lunbo/"; //String uploadFile = "/home/ftp/img/"+fileName; String uploadFile = "G:/ceshi/image/"+fileName; sftp=sf.connect(host, port, username, password); sf.upload(directory, uploadFile, sftp); sftp.getSession().disconnect(); sftp.quit(); sftp.disconnect();//关闭 //sf.download(directory, downloadFile, saveFile, sftp); //sf.delete(directory, deleteFile, sftp); return 1; } catch (Exception e) { //sftp.getSession().disconnect(); sftp.quit(); sftp.disconnect();//关闭 return 0; }}要注意图片服务器使用ftp传输,同时要注意关闭ftp链接,保证最终的图片正常传输,<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="defaultEncoding" value="UTF-8"/></bean>
原文地址:https://www.cnblogs.com/xuanyuer/p/9880355.html
时间: 2024-11-05 19:36:52