老师这是怎么回事,后台也没有报错
package com.bjsxt.ego.manager.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.bjsxt.ego.beans.PictureResult;
import com.bjsxt.manager.service.ManagerItemService;
@Controller
public class ItemImageController {
@Autowired
private ManagerItemService managerItemService;
/**
* 处理图片上传请求
*/
@RequestMapping(value="pic/upload",produces=MediaType.APPLICATION_JSON_VALUE
+";charset=UTF-8")
@ResponseBody
public PictureResult picUpLoad(MultipartFile uploadFile) {
return managerItemService.uploadItemPic(uploadFile);
}
}
package com.bjsxt.ego.beans;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FtpUtils {
public static void main(String[] args) {
/**
* 完成图片上传,通过ftp将图片上传到图片服务器
*/
String hostname = "192.168.86.135";
int port = 21;
String username = "ftpuer";
String password = "xuxiaoqin";
String pathname = "/home/ftpuser/jd";
String remote="demo.jpg";
InputStream local =null;
try {
local = new FileInputStream("D:/笔记/第八章/第四章/02 软件/pic/1.jpg");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
FtpUtils a = new FtpUtils();
a.uploadFile(hostname, port, username, password, pathname, remote, local);
}
public static boolean uploadFile(String hostname,
int port, String username,
String password, String pathname,
String remote,InputStream local) {
Boolean flag = false;
try {
//创建FtpClient对象
FTPClient client = new FTPClient();
//建立和ftp服务器的连接
client.connect(hostname,port);
//登录ftp服务器
client.login(username, password);
//设置上传的文件的类型
client.setFileType(FTP.BINARY_FILE_TYPE);
//切换工作目录,文件上传后保存到那个目录
if(!client.changeWorkingDirectory(pathname)){
if(client.makeDirectory(pathname)){
client.changeWorkingDirectory(pathname);
}
}
local = new FileInputStream("D:/笔记/第八章/第四章/02 软件/pic/1.jpg");
//实现文件上传
client.storeFile(remote, local);
local.close();
client.logout();
client.disconnect();
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
}
@Override
public PictureResult uploadItemPic(MultipartFile file) {
// TODO Auto-generated method stub
Boolean flag = null;
String fileName = null;
try {
//获得信息的文件的名字
fileName = IDUtils.genImageName();
//获得上传的文件的原始名字
String oriName = file.getOriginalFilename();
//获得文件扩展名
String ext = oriName.substring(oriName.lastIndexOf("."));
fileName = fileName+ext;
InputStream local = file.getInputStream();
//实现文件上传到ftp
flag = FtpUtils.uploadFile(FTP_HOST, FTP_PORT, FTP_USERNAME,
FTP_PASSWORD, FTP_PATH, fileName, local);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
flag = false;
}
PictureResult result = null;
if(flag) {
result = new PictureResult();
result.setError(0);
result.setUrl(IMAGE_HTTP_PATH+"/"+fileName);
result.setMessage("ok");
System.out.println(1);
}else {
result = new PictureResult();
result.setError(1);
result.setUrl("url");
result.setMessage("error");
}
return result;
}
ego.zip

