java文件上传
来源:互联网 发布:关于钢琴的软件 编辑:程序博客网 时间:2024/06/05 18:18
文件上传
package com.dmm.android.controller;import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Date;import java.util.Iterator;import java.util.List;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.log4j.Logger;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.dmm.android.common.RootPath;@Controller@ResponseBody@RequestMapping("/form")public class FormController { private final Logger LOG = Logger.getLogger(FormController.class); @RequestMapping(value = "/getSC", produces = "multipart/form-data; charset=utf-8") public void getSC(HttpServletRequest req, HttpServletResponse resp){ String scTitle = null; System.out.println(ServletFileUpload.isMultipartContent(req)+"================================"); DiskFileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload sfu = new ServletFileUpload(factory); sfu.setSizeMax(10*1024*1024); sfu.setHeaderEncoding("utf-8"); List<String> listStr = new ArrayList<String>(); List<FileItem> fileItemList = null; try { fileItemList = sfu.parseRequest(req); Iterator<FileItem> fileItems = fileItemList.iterator(); while(fileItems.hasNext()) { FileItem fileItem = fileItems.next(); if(fileItem.isFormField()){ String name = fileItem.getFieldName();// name属性值 String value = fileItem.getString("utf-8");// name对应的value值 if(name.equals("text")) { scTitle = value; } System.out.println(name + " = " + value); LOG.info(name + " = " + value); } else { String fileName = fileItem.getName(); System.out.println("原文件名:" + fileName); String suffix = fileName.substring(fileName.lastIndexOf('.')); System.out.println("扩展名:" + suffix); String newFileName = new Date().getTime() + suffix; System.out.println("新文件名:" + newFileName); listStr.add(newFileName); // 5. 调用FileItem的write()方法,写入文件 String imgPath = "/"+RootPath.getPath()+"/Temp/" + newFileName; LOG.info(imgPath); File file = new File(imgPath); System.out.println(file.getAbsolutePath()); LOG.info(file.getAbsolutePath()); fileItem.write(file); // 6. 调用FileItem的delete()方法,删除临时文件 fileItem.delete(); } } } catch (FileUploadException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }}
文件目录
package com.dmm.android.common;import java.io.Serializable;public final class RootPath implements Serializable { private static final long serialVersionUID = -7178694607074894676L; private static String path; private RootPath() { } public static String getPath() { path = new RootPath().getClass().getResource("").toString(); path = path.replace("%20", " ").replaceAll("file:/", "").replaceAll("/WEB-INF.*", ""); return path.replaceAll("^(home)", "/$1"); } public static String getConfigPath() { return RootPath.getPath() + "/WEB-INF/config"; }}
阅读全文
0 1
- Java 文件上传 JQ 文件上传
- java文件上传原理
- java文件上传原理
- java上传文件
- 上传文件 JSP+java
- JAVA上传文件
- java上传文件
- java fileupload上传文件
- java-文件上传案例
- JAVA上传EXCEL文件
- java上传文件
- java 超大文件上传
- java上传文件实例
- java 文件上传
- JAVA 上传文件类
- JAVA之"文件上传"
- java文件上传
- java文件上传方法
- set 和 map
- java 读取配置文件
- 机器学习博客汇总
- 44 C语言从键盘输入数据
- Portal for ArcGIS是什么东东
- java文件上传
- mysql 小结
- 在线编程(二)
- Http请求中Content-Type讲解以及在Spring MVC中的应用
- 解决ausu MT7630 linux(ubuntu)下 wifi无法使用
- KMP算法
- Android Studio 2.2
- 【php】关键函数备忘录
- 为什么const对象只能调用const成员函数,而不能调用非const成员函数?