文件、ckeditor放在服务器(虚拟机)上的操作

it2023-11-09  75

第一步:单独写一个commen的包,然后创一个FastdfsClient的class

package bootpanda.common; import java.io.IOException; import java.net.URLDecoder; import org.csource.common.MyException; import org.csource.common.NameValuePair; import org.csource.fastdfs.*; /** * fastdfs客户端 * * @author zw * */ public class FastDFSClient { private TrackerClient trackerClient = null; private TrackerServer trackerServer = null; private StorageServer storageServer = null; private StorageClient1 storageClient = null; /** * 调用本类时,需要在resources中增加fdfs_client.properties配置文件 * * @throws Exception */ public FastDFSClient() { try { String conf = this.getClass().getResource("/fdfs_client.properties").getPath(); if (conf.contains("classpath:")) { String path = URLDecoder .decode(getClass().getProtectionDomain().getCodeSource().getLocation().toString(), "UTF-8"); path = path.substring(6); conf = conf.replace("classpath:", URLDecoder.decode(path, "UTF-8")); } ClientGlobal.init(conf); trackerClient = new TrackerClient(); trackerServer = trackerClient.getConnection(); storageServer = null; storageClient = new StorageClient1(trackerServer, storageServer); } catch (Exception e) { e.printStackTrace(); } } /** * 上传文件方法 * <p> * Title: uploadFile * </p> * <p> * Description: * </p> * * @param fileName * 文件全路径 * @param extName * 文件扩展名,不包含(.) * @param metas * 文件扩展信息 * @return * @throws Exception */ public String uploadFile(String fileName, String extName, NameValuePair[] metas) { String result = null; try { result = storageClient.upload_file1(fileName, extName, metas); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return result; } /** * 上传文件,传fileName * * @param fileName * 文件的磁盘路径名称 如:D:/image/aaa.jpg * @return null为失败 */ public String uploadFile(String fileName) { return uploadFile(fileName, null, null); } /** * * @param fileName * 文件的磁盘路径名称 如:D:/image/aaa.jpg * @param extName * 文件的扩展名 如 txt jpg等 * @return null为失败 */ public String uploadFile(String fileName, String extName) { return uploadFile(fileName, extName, null); } /** * 上传文件方法 * <p> * Title: uploadFile * </p> * <p> * Description: * </p> * * @param fileContent * 文件的内容,字节数组 * @param extName * 文件扩展名 * @param metas * 文件扩展信息 * @return * @throws Exception */ public String uploadFile(byte[] fileContent, String extName, NameValuePair[] metas) { String result = null; try { result = storageClient.upload_file1(fileContent, extName, metas); } catch (IOException e) { e.printStackTrace(); } catch (MyException e) { e.printStackTrace(); } return result; } /** * 上传文件 * * @param fileContent * 文件的字节数组 * @return null为失败 * @throws Exception */ public String uploadFile(byte[] fileContent) { return uploadFile(fileContent, null, null); } /** * 上传文件 * * @param fileContent * 文件的字节数组 * @param extName * 文件的扩展名 如 txt jpg png 等 * @return null为失败 */ public String uploadFile(byte[] fileContent, String extName) { return uploadFile(fileContent, extName, null); } }

第二步:在创一个tool类

package bootpanda.common; import java.io.File; import java.io.IOException; import java.util.Enumeration; import java.util.Properties; import java.util.UUID; import javax.servlet.http.HttpServletRequest; import org.springframework.web.multipart.MultipartFile; /** * 工具类 * @author zw * */ public class Tools { /** * 上传文件的方法。 * @param file * @param request * @return 返回文件的相对路径 */ public static String uploadfile(MultipartFile file,HttpServletRequest request){ String fileName = ""; if (file != null && file.getSize() > 0) { // 处理文件上传 // 获取文件名称(获取的是:文件名.后缀名) String oriName = file.getOriginalFilename(); // 获取文件后缀名 String extName = oriName.substring(oriName.lastIndexOf(".")); // 获取文件名称(获取上传到服务器的文件名称和路径) // 上传文件的时候,文件名称一般可以使用UUID;或者当前时间的long表示(这种做法不合适大并发的应用) fileName = "files/" + UUID.randomUUID().toString() + extName; // 获取文件上传路径(物理路径) String uppath = request.getServletContext().getRealPath("/") + fileName; // 保存文件 try { file.transferTo(new File(uppath)); } catch (IllegalStateException e) { e.printStackTrace(); fileName = ""; } catch (IOException e) { e.printStackTrace(); fileName = ""; } } return fileName; } /** * 获取文件服务器地址 * @return */ public static String getFileServer(){ Properties p = new Properties(); String fileserver = ""; try { p.load(Tools.class.getClassLoader().getResourceAsStream("fileserver.properties")); fileserver = p.getProperty("fileserver"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return fileserver; } }

第三步:resource下创建fdfs_client.properties的配置文件

connect_timeout = 2 network_timeout = 30 charset = UTF-8 # tracker Http port http.tracker_http_port = 80 http.anti_steal_token = no http.secret_key = FastDFS1234567890 # tracker Server address tracker_server = 192.168.1.100:22122

第四步:resource下创建fileserver.properties的配置对象

fileserver=http://192.168.1.100/

改成自己的ip地址

调用

@RequestMapping("/add") public String add(Model model, SysHotelType type, Hotel hotel, MultipartFile file, HttpServletRequest request) { // 只要是处理页面传过来的数据,都在这里处理,而尽量不在接口实现类处理 hotel.setHotelid(UUID.randomUUID().toString()); String[] hotels = hotel.getHoteltypename().split(","); hotel.setHoteltypeid(hotels[0]); System.out.println(hotels[0]); hotel.setHoteltypename(hotels[1]); // 上传文件到fastdfs上======== if (file != null && file.getSize() > 0) { FastDFSClient client = new FastDFSClient(); String filename = file.getOriginalFilename(); String extname = filename.substring(filename.lastIndexOf(".") + 1); // 不包括. try { String filepath = client.uploadFile(file.getBytes(), extname); // 调用tool里面的fastdfs类的方法 hotel.setHotelimg(filepath); } catch (IOException e) { e.printStackTrace(); } } int rtn = 0; if (hotel != null && hotel.getHotelname()!=null && !hotel.getHotelname().equals("")) { rtn = service.insertHotel(hotel); } if (rtn > 0) { model.addAttribute("msg", "增加酒店成功"); } else { model.addAttribute("msg", "增加酒店失败"); } return toadd(model, type); } model.addAttribute("fileserver", Tools.getFileServer());

ckeditor

ckeditor–>skins–>config.js 修改里面的

config.filebrowserImageUploadUrl ='/PANDA/ckeditor/upload' ;

对应下面的controller

ckeditor的controller

package bootpanda.controller; import java.io.PrintWriter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; import bootpanda.common.FastDFSClient; import bootpanda.common.Tools; //富文本编辑器上传文件到fastdfs服务器 //用于ckeditor的文件上传 @Controller @RequestMapping("/ckeditor") public class UpLoadFileController { @RequestMapping("/upload") public void uploadimg(@RequestParam("upload") MultipartFile file, HttpServletRequest request,HttpServletResponse response) throws Exception { //上传文件 FastDFSClient dfs = new FastDFSClient(); String filename = file.getOriginalFilename(); String extName = filename.substring(filename.lastIndexOf(".") + 1); String url = dfs.uploadFile(file.getBytes(), extName); //构建文件的完整路径。包含:文件服务器地址+文件位置 url = Tools.getFileServer() + url; //需要向客户端响应如下消息 response.setContentType("text/html;charset=UTF-8"); String callback = request.getParameter("CKEditorFuncNum"); PrintWriter out = response.getWriter(); out.println("<script type=\"text/javascript\">"); out.println("window.parent.CKEDITOR.tools.callFunction(" + callback + ",'" + url + "',''" + ")"); out.println("</script>"); out.flush(); out.close(); } }

在jsp页面中,注意ckeditor需要下面四点

1

<script type="text/javascript" src="${pageContext.request.contextPath}/ckeditor/ckeditor.js"></script>

2

method="post" enctype="multipart/form-data"

3

<textarea id="himgdesc" class="ckeditor" name="himgdesc"></textarea>

4

<script type="text/javascript"> CKEDITOR.replace('spotsdesc'); </script> </body>
最新回复(0)