图片上传返回上传链接

@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 = ;
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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值