继前面的甘特图之后,这次来说说wex5中如何实现简单的文件上传和删除,直接上代码
文件上传
public static JSONObject FileUpload(JSONObject params, ActionContext context) { JSONObject ret = new JSONObject(); HttpServletRequest request = (HttpServletRequest) context.get(ActionContext.REQUEST); if (!request.getMethod().equals("POST")) { // 非POST直接空内容响应 return null; } if (isfirst) { String baasPath = request.getSession().getServletContext().getRealPath("/") + ".." + File.separator + ".."; docStorePath = baasPath + File.separator + "data" + File.separator + "Files"; System.out.println(docStorePath); File file = new File(docStorePath); if (!(file.exists() && file.isDirectory())) { file.mkdirs(); } try { docStorePath = file.getCanonicalPath(); } catch (IOException e) { e.printStackTrace(); } } try { String FILETYPE = request.getParameter("filetype");// 文件类型 String CATALOG = request.getParameter("catalog");//文件分类 docStorePath = docStorePath + File.separator + CATALOG; File file = new File(docStorePath); if (!(file.exists() && file.isDirectory())) { file.mkdirs(); } String baasPath = request.getSession().getServletContext().getRealPath("/") + ".." + File.separator + ".."; String tdocStorePath = baasPath + File.separator + "data" + File.separator + "Files"; String tempPath = tdocStorePath + "temp"; File tmpFile = new File(tempPath); if (!tmpFile.exists()) { // 创建临时目录 tmpFile.mkdir(); } DiskFileItemFactory factory = new DiskFileItemFactory();// 创建DiskFileItemFactory文件项工厂对象 factory.setSizeThreshold(1024 * 100); factory.setRepository(tmpFile); ServletFileUpload upload = new ServletFileUpload(factory);// 通过工厂对象获取文件上传请求核心解析类ServletFileUpload List<FileItem> list = upload.parseRequest(request);// 使用ServletFileUpload对应Request对象进行解析 for (FileItem item : list) { // 如果fileitem中封装的是普通输入项的数据(是否是上传项) if (item.isFormField()) {//不是上传项 } else {// 如果fileitem中封装的是上传文件 // 获取item中的上传文件的输入流 InputStream in = item.getInputStream(); // if(FILETYPE.equals("5.png")){ // FILETYPE=1+"_"+"5.png"; // } // 创建一个文件输出流 FileOutputStream out = new FileOutputStream(docStorePath + "\\" + FILETYPE); // 创建一个缓冲区 byte buffer[] = new byte[1024]; // 判断输入流中的数据是否已经读完的标识 int len = 0; // 循环将输入流读入到缓冲区当中,(len=in.read(buffer))>0就表示in里面还有数据 while ((len = in.read(buffer)) > 0) { // 使用FileOutputStream输出流将缓冲区的数据写入到指定的目录当中 out.write(buffer, 0, len); } // 关闭输入流 in.close(); // 关闭输出流 out.close(); // 删除处理文件上传时生成的临时文件 item.delete(); } } } catch (Exception e) { ret.put("message", e.getMessage()); } return ret; }
删除
public static JSONObject deleteFile(JSONObject params, ActionContext context) throws SQLException, NamingException, NoSuchAlgorithmException { // Connection conn = context.getConnection(DATASOURCE_BOAT); JSONObject rt = new JSONObject(); String fname = params.getString("fname"); String baasPath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + ".." + File.separator + ".."; String docStorePath = baasPath + File.separator + "data" + File.separator + "Files" + File.separator + fname; File file = new File(docStorePath); if (file.exists()) { file.delete(); rt.put("count", "0");// 删除成功 } else { rt.put("count", "1");// 删除失败 } return rt; }
前台js代码
var self = this; var version; self.uploader = new Uploader(this.getElementByXid("button5")); //打开上传选择框 self.uploader.on('onStart', function(event) {//打开选择框 //可自己做操作 //设置上传地址 self.uploader.actionUrl = "/baas/common/fileapi/basimageupload?filetype=" + newname + "&catalog=folderContent&length=" + event.file.size; }); //上传成功 self.uploader.on('onSuccess', function(event) {//上传成功 justep.Util.hint("上传成功!"); //可自己做操作 }); //上传失败 self.uploader.on('onError', function(event) { justep.Util.hint("上传失败!"); //可自己做操作 }); //引用js var Uploader = require('$UI/system/components/justep/uploader/uploader');
好了,就上传简单的文件上传和删除,其他的就不上传了,当然这是我自己写的方法,其实平台里面也有现成的方法,自己可以稍加改用,就可以实现上传下载删除和查看了
关于wex5中如何实现文件上传
猜你喜欢
转载自blog.csdn.net/CX_silent/article/details/85046157
今日推荐
周排行