Commit 012b35e3 authored by killfen's avatar killfen

上传路径问题修复

parent 21de2132
...@@ -35,38 +35,45 @@ import org.apache.commons.fileupload.FileItem; ...@@ -35,38 +35,45 @@ import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletFileUpload;
import com.mingsoft.constant.Const;
import com.mingsoft.util.FileUtil; import com.mingsoft.util.FileUtil;
import com.mingsoft.util.StringUtil; import com.mingsoft.util.StringUtil;
/** /**
* 文件上传通用servlet * 文件上传通用servlet
*
* @author 王天培QQ:78750478 * @author 王天培QQ:78750478
* @version * @version 版本号:100-000-000<br/>
* 版本号:100-000-000<br/> * 创建日期:2012-03-15<br/>
* 创建日期:2012-03-15<br/> * 历史修订:<br/>
* 历史修订:<br/>
*/ */
@WebServlet(urlPatterns = "/upload") @WebServlet(urlPatterns = "/upload")
public class UploadServlet extends BaseServlet { public class UploadServlet extends BaseServlet {
/** /**
* 处理post请求上传文件 * 处理post请求上传文件
* @param req HttpServletRequest对象 *
* @param res HttpServletResponse 对象 * @param req
* @throws ServletException 异常处理 * HttpServletRequest对象
* @throws IOException 异常处理 * @param res
* HttpServletResponse 对象
* @throws ServletException
* 异常处理
* @throws IOException
* 异常处理
*/ */
@Override @Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter out = res.getWriter(); PrintWriter out = res.getWriter();
String uploadPath = this.getServletContext().getRealPath(File.separator); // 上传的文件路径 String uploadPath = this.getServletContext().getRealPath(Const.SEPARATOR); // 上传的文件路径
String isRename = "";// 是否重命名 true:重命名 String isRename = "";// 是否重命名 true:重命名
String _tempPath = req.getServletContext().getRealPath(File.separator) + "temp";//存放文件的临时目录路径 String _tempPath = req.getServletContext().getRealPath(Const.SEPARATOR) + "temp";// 存放文件的临时目录路径
FileUtil.createFolder(_tempPath); FileUtil.createFolder(_tempPath);
File tempPath = new File(_tempPath); // 用于存放临时文件的目录 File tempPath = new File(_tempPath); // 用于存放临时文件的目录
int maxSize = 1000000; // 允许上传文件大小,最大上传文件,单位:字节 1000000/1024=0.9M int maxSize = 1000000; // 允许上传文件大小,最大上传文件,单位:字节 1000000/1024=0.9M
//String allowedFile = ".jpg,.gif,.png,.zip"; // 允许上传文件 // String allowedFile = ".jpg,.gif,.png,.zip"; // 允许上传文件
String deniedFile = ".exe,.com,.cgi,.asp"; // 不允许上传文件 String deniedFile = ".exe,.com,.cgi,.asp"; // 不允许上传文件
DiskFileItemFactory factory = new DiskFileItemFactory(); DiskFileItemFactory factory = new DiskFileItemFactory();
...@@ -79,10 +86,10 @@ public class UploadServlet extends BaseServlet { ...@@ -79,10 +86,10 @@ public class UploadServlet extends BaseServlet {
ServletFileUpload upload = new ServletFileUpload(factory); ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown // maximum size before a FileUploadException will be thrown
try { try {
List fileItems = upload.parseRequest(req); List fileItems = upload.parseRequest(req);
Iterator iter = fileItems.iterator(); Iterator iter = fileItems.iterator();
// 正则匹配,过滤路径取文件名 // 正则匹配,过滤路径取文件名
...@@ -91,19 +98,19 @@ public class UploadServlet extends BaseServlet { ...@@ -91,19 +98,19 @@ public class UploadServlet extends BaseServlet {
// 过滤掉的文件类型 // 过滤掉的文件类型
String[] errorType = deniedFile.split(","); String[] errorType = deniedFile.split(",");
Pattern p = Pattern.compile(regExp); Pattern p = Pattern.compile(regExp);
String outPath = ""; //输出保存后的图片路径 String outPath = ""; // 输出保存后的图片路径
while (iter.hasNext()) { while (iter.hasNext()) {
FileItem item = (FileItem) iter.next(); FileItem item = (FileItem) iter.next();
if (item.getFieldName().equals("uploadPath")) { if (item.getFieldName().equals("uploadPath")) {
outPath += item.getString(); outPath += item.getString();
uploadPath += outPath; uploadPath += outPath;
} else if (item.getFieldName().equals("isRename")) { } else if (item.getFieldName().equals("isRename")) {
isRename = item.getString(); isRename = item.getString();
} else if (item.getFieldName().equals("maxSize")) { } else if (item.getFieldName().equals("maxSize")) {
maxSize = Integer.parseInt(item.getString())*1048576; maxSize = Integer.parseInt(item.getString()) * 1048576;
} else if (item.getFieldName().equals("allowedFile")) { } else if (item.getFieldName().equals("allowedFile")) {
// allowedFile = item.getString(); // allowedFile = item.getString();
} else if (item.getFieldName().equals("deniedFile")) { } else if (item.getFieldName().equals("deniedFile")) {
deniedFile = item.getString(); deniedFile = item.getString();
} else if (!item.isFormField()) { // 忽略其他不是文件域的所有表单信息 } else if (!item.isFormField()) { // 忽略其他不是文件域的所有表单信息
...@@ -114,11 +121,11 @@ public class UploadServlet extends BaseServlet { ...@@ -114,11 +121,11 @@ public class UploadServlet extends BaseServlet {
try { try {
// 最大上传文件,单位:字节 1000000/1024=0.9M // 最大上传文件,单位:字节 1000000/1024=0.9M
upload.setSizeMax(maxSize); upload.setSizeMax(maxSize);
// 保存上传的文件到指定的目录 // 保存上传的文件到指定的目录
// 在下文中上传文件至数据库时,将对这里改写 // 在下文中上传文件至数据库时,将对这里改写
String fileName = System.currentTimeMillis() + name.substring(name.indexOf(".")); String fileName = System.currentTimeMillis() + name.substring(name.indexOf("."));
String savePath = uploadPath + File.separator; String savePath = uploadPath + Const.SEPARATOR;
FileUtil.createFolder(savePath); FileUtil.createFolder(savePath);
// 重命名 // 重命名
if (StringUtil.isBlank(isRename) || Boolean.parseBoolean(isRename)) { if (StringUtil.isBlank(isRename) || Boolean.parseBoolean(isRename)) {
...@@ -143,20 +150,4 @@ public class UploadServlet extends BaseServlet { ...@@ -143,20 +150,4 @@ public class UploadServlet extends BaseServlet {
this.logger.debug(e); this.logger.debug(e);
} }
} }
// /**
// * 处理get请求上传文件
// * @param request HttpServletRequest对象
// * @param response HttpServletResponse 对象
// * @throws ServletException Servlet异常处理
// * @throws IOException IO异常处理
// */
// @Override
// protected void doGet(HttpServletRequest request, HttpServletResponse response)
// throws ServletException, IOException {
// String uploadPath = request.getParameter("uploadPath"); // 上传的文件路径
// String fileSize = request.getParameter("fileSize"); // 上传的文件大小
// String fileType = request.getParameter("fileType"); // 上传的文件类型
// String deniedFileType = request.getParameter("deniedFileType"); // 不允许上传的文件类型,
// }
} }
\ No newline at end of file
...@@ -19,12 +19,12 @@ The MIT License (MIT) * Copyright (c) 2015 铭飞科技 ...@@ -19,12 +19,12 @@ The MIT License (MIT) * Copyright (c) 2015 铭飞科技
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
package com.mingsoft.constant; package com.mingsoft.constant;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
/** /**
* *
* *
...@@ -118,7 +118,12 @@ public final class Const { ...@@ -118,7 +118,12 @@ public final class Const {
/** 微信相关结束 */ /** 微信相关结束 */
public final static String UTF8 = "utf-8"; public final static String UTF8 = "utf-8";
/**
* 文件路径符
*/
public final static String SEPARATOR ="/";
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment