Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
MCMS
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
MCMS
Commits
012b35e3
Commit
012b35e3
authored
Dec 30, 2015
by
killfen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
上传路径问题修复
parent
21de2132
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
40 additions
and
44 deletions
+40
-44
src/main/java/com/mingsoft/basic/servlet/UploadServlet.java
src/main/java/com/mingsoft/basic/servlet/UploadServlet.java
+28
-37
src/main/java/com/mingsoft/constant/Const.java
src/main/java/com/mingsoft/constant/Const.java
+12
-7
No files found.
src/main/java/com/mingsoft/basic/servlet/UploadServlet.java
View file @
012b35e3
...
...
@@ -35,14 +35,16 @@ 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
com.mingsoft.constant.Const
;
import
com.mingsoft.util.FileUtil
;
import
com.mingsoft.util.StringUtil
;
/**
* 文件上传通用servlet
*
* @author 王天培QQ:78750478
* @version
* 版本号:100-000-000<br/>
* @version 版本号:100-000-000<br/>
* 创建日期:2012-03-15<br/>
* 历史修订:<br/>
*/
...
...
@@ -51,22 +53,27 @@ public class UploadServlet extends BaseServlet {
/**
* 处理post请求上传文件
* @param req HttpServletRequest对象
* @param res HttpServletResponse 对象
* @throws ServletException 异常处理
* @throws IOException 异常处理
*
* @param req
* HttpServletRequest对象
* @param res
* HttpServletResponse 对象
* @throws ServletException
* 异常处理
* @throws IOException
* 异常处理
*/
@Override
protected
void
doPost
(
HttpServletRequest
req
,
HttpServletResponse
res
)
throws
ServletException
,
IOException
{
PrintWriter
out
=
res
.
getWriter
();
String
uploadPath
=
this
.
getServletContext
().
getRealPath
(
File
.
separator
);
// 上传的文件路径
String
uploadPath
=
this
.
getServletContext
().
getRealPath
(
Const
.
SEPARATOR
);
// 上传的文件路径
String
isRename
=
""
;
// 是否重命名 true:重命名
String
_tempPath
=
req
.
getServletContext
().
getRealPath
(
File
.
separator
)
+
"temp"
;
//
存放文件的临时目录路径
String
_tempPath
=
req
.
getServletContext
().
getRealPath
(
Const
.
SEPARATOR
)
+
"temp"
;
//
存放文件的临时目录路径
FileUtil
.
createFolder
(
_tempPath
);
File
tempPath
=
new
File
(
_tempPath
);
// 用于存放临时文件的目录
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"
;
// 不允许上传文件
DiskFileItemFactory
factory
=
new
DiskFileItemFactory
();
...
...
@@ -91,7 +98,7 @@ public class UploadServlet extends BaseServlet {
// 过滤掉的文件类型
String
[]
errorType
=
deniedFile
.
split
(
","
);
Pattern
p
=
Pattern
.
compile
(
regExp
);
String
outPath
=
""
;
//输出保存后的图片路径
String
outPath
=
""
;
//
输出保存后的图片路径
while
(
iter
.
hasNext
())
{
FileItem
item
=
(
FileItem
)
iter
.
next
();
...
...
@@ -101,9 +108,9 @@ public class UploadServlet extends BaseServlet {
}
else
if
(
item
.
getFieldName
().
equals
(
"isRename"
))
{
isRename
=
item
.
getString
();
}
else
if
(
item
.
getFieldName
().
equals
(
"maxSize"
))
{
maxSize
=
Integer
.
parseInt
(
item
.
getString
())
*
1048576
;
maxSize
=
Integer
.
parseInt
(
item
.
getString
())
*
1048576
;
}
else
if
(
item
.
getFieldName
().
equals
(
"allowedFile"
))
{
//
allowedFile = item.getString();
//
allowedFile = item.getString();
}
else
if
(
item
.
getFieldName
().
equals
(
"deniedFile"
))
{
deniedFile
=
item
.
getString
();
}
else
if
(!
item
.
isFormField
())
{
// 忽略其他不是文件域的所有表单信息
...
...
@@ -118,7 +125,7 @@ public class UploadServlet extends BaseServlet {
// 保存上传的文件到指定的目录
// 在下文中上传文件至数据库时,将对这里改写
String
fileName
=
System
.
currentTimeMillis
()
+
name
.
substring
(
name
.
indexOf
(
"."
));
String
savePath
=
uploadPath
+
File
.
separator
;
String
savePath
=
uploadPath
+
Const
.
SEPARATOR
;
FileUtil
.
createFolder
(
savePath
);
// 重命名
if
(
StringUtil
.
isBlank
(
isRename
)
||
Boolean
.
parseBoolean
(
isRename
))
{
...
...
@@ -143,20 +150,4 @@ public class UploadServlet extends BaseServlet {
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
src/main/java/com/mingsoft/constant/Const.java
View file @
012b35e3
...
...
@@ -120,5 +120,10 @@ public final class Const {
public
final
static
String
UTF8
=
"utf-8"
;
/**
* 文件路径符
*/
public
final
static
String
SEPARATOR
=
"/"
;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment