使用Fck上传图片
程序员文章站
2022-03-20 12:16:56
...
controller代码
@RequestMapping("/upload/uploadFck.do")
public void uploadFck(HttpServletRequest request,HttpServletResponse response) throws UniformInterfaceException, ClientHandlerException, IOException{
MultipartRequest mr = (MultipartRequest)request;
Map<String, MultipartFile> map = mr.getFileMap();
Set<String> set = map.keySet();
MultipartFile pic = null;
for(String key : set){
pic = map.get(key);
}
String ext = FilenameUtils.getExtension(pic.getOriginalFilename());
// 生成路径地址
DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String format = dateFormat.format(new Date());
// 实例化Jersey
Client client = new Client();
// 保存数据库路径
Random r = new Random();
for (int i = 0; i < 3; i++) {
format += r.nextInt(9);
}
String path = "upload/" + format + "." + ext;
// 另一台服务器请求路径
String url = Constants.IMAGE_URL + path;
// 设置请求路径
WebResource resource = client.resource(url);
// 读图片到内存中
// 发送开始PUT
resource.put(String.class, pic.getBytes());
UploadResponse ok = UploadResponse.getOK(url);
response.getWriter().print(ok);
}
//前台代码加入
fck.Config["ImageUploadURL"] = "/upload/uploadFck.do";