上传文件后台代码
2019-06-14 21:10
78 查看
import net.sf.json.JSONObject; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; /** * <p> * 上传文件 * </p> * * @author zz * @since 2019/4/25 */ @Configuration public class UploadUtils { public static String getUploadedFolder() { return TbPageUtils.getBxImgPath(); } /** * @Author zz * @Description 上传文件 * @param * @return */ public static JSONObject uploadFile(HttpServletRequest request, MultipartFile file) { JSONObject result = new JSONObject(); try { //判断文件是否为空 if (file.isEmpty()) { result.put("success", false); result.put("message", "文件为空"); } long fileSize = file.getSize(); if(fileSize > 314572800) { result.put("success", false); result.put("message", "文件不能超过300MB"); } String fileName = file.getOriginalFilename(); String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + "_" + fileName; String path = TbPageUtils.getBxImgPath() + "/"+newFileName; File checkFile = new File(path); if(checkFile.exists()){ result.put("success", false); result.put("message", "文件已存在"); } if (!checkFile.getParentFile().exists()) { checkFile.getParentFile().mkdir(); } //保存文件 file.transferTo(checkFile); result.put("success", true); result.put("message", "上传成功"); result.put("fileName", newFileName); } catch (IOException e) { e.printStackTrace(); result.put("success", false); result.put("message", "上传失败"); } return result; } /** * @Author zz * @Description 删除文件 * @param * @return */ public static JSONObject deleteFile(String fileName) { JSONObject result = new JSONObject(); String path = TbPageUtils.getBxImgPath() +"\\"+ fileName; File file = new File(path); boolean flag = false; if (!file.exists()) { result.put("message", "文件不存在"); result.put("success", flag); } try{ flag = file.delete(); }catch (Exception e){ e.printStackTrace(); } result.put("message", "删除成功"); result.put("success", flag); return result; } }
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import net.sf.json.JSONObject; /** * <p> * 上传文件API * </p> * * @author zz * @since 2019/4/26 */ @RestController @RequestMapping("/uploadApi") public class UploadApi { /** * @Author zz * @Description 上传文件 * @param request * @param response * @param file * @return JSONObject */ @RequestMapping("/uploadFile") public JSONObject uploadFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("file") MultipartFile file) { // 允许跨域请求 response.setHeader("Access-Control-Allow-Origin","*"); return UploadUtils.uploadFile(request, file); } /** * @Author zz * @Description 删除文件 * @param fileName 文件名称 * @return JSONObject */ @RequestMapping("/deleteFile") public JSONObject deleteFile(@RequestParam("fileName") String fileName) { return UploadUtils.deleteFile(fileName); } }
相关文章推荐
- Ext 上传文件,前后台实现, Asp.net 代码
- formdata ajax上传文件前后台代码
- VC调用jsp上传文件到JAVA后台代码
- Ext 上传文件,前后台实现, Asp.net 代码
- uploadify上传及后台文件合法性验证的代码解析
- 文件上传——java后台代码
- 多文件上传的后台代码实现
- 使用extjs上传文件到java后台的完整代码案例
- 浅谈 Html5 带进度条的的文件图片(带本地回显)上传(后台代码)
- Apache Cordova 安卓android上传图片或者文件 调用后台服务实现java代码 ft.upload(imageURI, uri, that.uploadSuccess, that.
- flash8上传文件后台代码-.Net,php,cf
- Ext 上传文件,前后台实现, Asp.net 代码
- asp.net 下载任意格式文件 上传文件后台代码
- webuploader+springmvc实现多文件上传(html+js+css原创,后台代码借鉴)
- [代码审计]XiaoCms(后台任意文件上传至getshell,任意目录删除,会话固定漏洞)
- 文件上传的 接收后台代码(java)
- webuploader+springmvc实现多文件上传(html+js+css原创,后台代码借鉴)
- javaweb上传文件前后台代码示例
- C#后台代码 Post 提交表单上传文件及其他信息
- java实现文件的上传、下载和删除的后台代码