图片上传返回上传链接

Posted xuanyuer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图片上传返回上传链接相关的知识,希望对你有一定的参考价值。

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





















































































以上是关于图片上传返回上传链接的主要内容,如果未能解决你的问题,请参考以下文章

图片上传返回URL接口

H5 图片上传

上传图片显示本地链接——FileReader

ckeditor 上传图片 php版

uni-app 微信小程序 上传图片 文件 uni.uploadFile() 、 uni.chooseImage()

55bbs论坛上传图片,显示输入图片链接地址,找不到附件功能。