Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
ry-exchange
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
王金锋
ry-exchange
Commits
72895b1d
Commit
72895b1d
authored
Mar 21, 2018
by
wangjf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
976f9534
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
135 additions
and
4 deletions
+135
-4
FileUploadConfiguration.java
.../com/reyun/dmp/configuration/FileUploadConfiguration.java
+28
-2
FileUploadController.java
...n/java/com/reyun/dmp/controller/FileUploadController.java
+107
-2
No files found.
src/main/java/com/reyun/dmp/configuration/FileUploadConfiguration.java
View file @
72895b1d
package
com
.
reyun
.
dmp
.
configuration
;
import
javax.servlet.MultipartConfigElement
;
import
org.springframework.boot.web.servlet.MultipartConfigFactory
;
import
org.springframework.context.annotation.Bean
;
/** * 文件上传配置 * * @author wangjf * @date 2018年03月21日15:15:20 */
public
class
FileUploadConfiguration
{
@Bean
public
MultipartConfigElement
multipartConfigElement
()
{
MultipartConfigFactory
factory
=
new
MultipartConfigFactory
();
// 设置文件大小限制 ,超出设置页面会抛出异常信息, // 这样在文件上传的地方就需要进行异常信息的处理了; factory.setMaxFileSize("256KB"); // KB,MB /// 设置总上传数据总大小 factory.setMaxRequestSize("512KB"); // Sets the directory location where files will be stored. // factory.setLocation("路径地址"); return factory.createMultipartConfig(); } }
package
com
.
reyun
.
dmp
.
configuration
;
\ No newline at end of file
import
javax.servlet.MultipartConfigElement
;
import
org.springframework.boot.web.servlet.MultipartConfigFactory
;
import
org.springframework.context.annotation.Bean
;
/**
* 文件上传配置
*
* @author wangjf
* @date 2018年03月21日15:15:20
*/
public
class
FileUploadConfiguration
{
@Bean
public
MultipartConfigElement
multipartConfigElement
()
{
MultipartConfigFactory
factory
=
new
MultipartConfigFactory
();
// 设置文件大小限制 ,超出设置页面会抛出异常信息,
// 这样在文件上传的地方就需要进行异常信息的处理了;
factory
.
setMaxFileSize
(
"256KB"
);
// KB,MB
/// 设置总上传数据总大小
factory
.
setMaxRequestSize
(
"512KB"
);
// Sets the directory location where files will be stored.
// factory.setLocation("路径地址");
return
factory
.
createMultipartConfig
();
}
}
src/main/java/com/reyun/dmp/controller/FileUploadController.java
View file @
72895b1d
package
com
.
reyun
.
dmp
.
controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.*
;
import
java.util.List
;
/** * 文件上传controller * * @author wangjf * @email wangjinfeng@reyun.com * @date 2018年03月21日15:10:13 */
@Controller
public
class
FileUploadController
{
// 单文件访问路径为:http://ip:port/upload @RequestMapping(value = "/upload", method = RequestMethod.GET) public String upload() { return "/fileupload"; } // 批量文件访问路径为:http://ip:port/upload/batch @RequestMapping(value = "/upload/batch", method = RequestMethod.GET) public String batchUpload() { return "/mutifileupload"; } /** * * 文件上传具体实现方法(单文件) * * @param account_id * @param user_id_type * @param tag_id * @param open_app_id * @param file * @return */ @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public String upload(@RequestParam(value = "account_id", required = false) String account_id, @RequestParam(value = "user_id_type", required = false) String user_id_type, @RequestParam(value = "tag_id", required = false) String tag_id, @RequestParam(value = "open_app_id", required = false) String open_app_id, @RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { try { // 这里只是简单例子,文件直接输出到项目路径下。 // 实际项目中,文件需要输出到指定位置,需要在增加代码处理。 // 还有关于文件格式限制、文件大小限制,详见:中配置。 BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(new File(file.getOriginalFilename()))); out.write(file.getBytes()); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return "上传失败," + e.getMessage(); } catch (IOException e) { e.printStackTrace(); return "上传失败," + e.getMessage(); } return "上传成功"; } else { return "上传失败,因为文件是空的."; } } /** * 多文件上传 主要是使用了MultipartHttpServletRequest和MultipartFile * * @param request * @return */ @RequestMapping(value = "/upload/batch", method = RequestMethod.POST) public @ResponseBody String batchUpload(HttpServletRequest request) { List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file"); MultipartFile file = null; BufferedOutputStream stream = null; for (int i = 0; i < files.size(); ++i) { file = files.get(i); if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); stream = new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename()))); stream.write(bytes); stream.close(); } catch (Exception e) { stream = null; return "You failed to upload " + i + " => " + e.getMessage(); } } else { return "You failed to upload " + i + " because the file was empty."; } } return "upload successful"; } }
package
com
.
reyun
.
dmp
.
controller
;
\ No newline at end of file
import
org.springframework.stereotype.Controller
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RequestMethod
;
import
org.springframework.web.bind.annotation.RequestParam
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartHttpServletRequest
;
import
javax.servlet.http.HttpServletRequest
;
import
java.io.*
;
import
java.util.List
;
/**
* 文件上传controller
*
* @author wangjf
* @email wangjinfeng@reyun.com
* @date 2018年03月21日15:10:13
*/
@Controller
public
class
FileUploadController
{
// 单文件访问路径为:http://ip:port/upload
@RequestMapping
(
value
=
"/upload"
,
method
=
RequestMethod
.
GET
)
public
String
upload
()
{
return
"/fileupload"
;
}
// 批量文件访问路径为:http://ip:port/upload/batch
@RequestMapping
(
value
=
"/upload/batch"
,
method
=
RequestMethod
.
GET
)
public
String
batchUpload
()
{
return
"/mutifileupload"
;
}
/**
*
* 文件上传具体实现方法(单文件)
*
* @param account_id
* @param user_id_type
* @param tag_id
* @param open_app_id
* @param file
* @return
*/
@RequestMapping
(
value
=
"/upload"
,
method
=
RequestMethod
.
POST
)
@ResponseBody
public
String
upload
(
@RequestParam
(
value
=
"account_id"
,
required
=
false
)
String
account_id
,
@RequestParam
(
value
=
"user_id_type"
,
required
=
false
)
String
user_id_type
,
@RequestParam
(
value
=
"tag_id"
,
required
=
false
)
String
tag_id
,
@RequestParam
(
value
=
"open_app_id"
,
required
=
false
)
String
open_app_id
,
@RequestParam
(
"file"
)
MultipartFile
file
)
{
if
(!
file
.
isEmpty
())
{
try
{
// 这里只是简单例子,文件直接输出到项目路径下。
// 实际项目中,文件需要输出到指定位置,需要在增加代码处理。
// 还有关于文件格式限制、文件大小限制,详见:中配置。
BufferedOutputStream
out
=
new
BufferedOutputStream
(
new
FileOutputStream
(
new
File
(
file
.
getOriginalFilename
())));
out
.
write
(
file
.
getBytes
());
out
.
flush
();
out
.
close
();
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
return
"上传失败,"
+
e
.
getMessage
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
"上传失败,"
+
e
.
getMessage
();
}
return
"上传成功"
;
}
else
{
return
"上传失败,因为文件是空的."
;
}
}
/**
* 多文件上传 主要是使用了MultipartHttpServletRequest和MultipartFile
*
* @param request
* @return
*/
@RequestMapping
(
value
=
"/upload/batch"
,
method
=
RequestMethod
.
POST
)
public
@ResponseBody
String
batchUpload
(
HttpServletRequest
request
)
{
List
<
MultipartFile
>
files
=
((
MultipartHttpServletRequest
)
request
).
getFiles
(
"file"
);
MultipartFile
file
=
null
;
BufferedOutputStream
stream
=
null
;
for
(
int
i
=
0
;
i
<
files
.
size
();
++
i
)
{
file
=
files
.
get
(
i
);
if
(!
file
.
isEmpty
())
{
try
{
byte
[]
bytes
=
file
.
getBytes
();
stream
=
new
BufferedOutputStream
(
new
FileOutputStream
(
new
File
(
file
.
getOriginalFilename
())));
stream
.
write
(
bytes
);
stream
.
close
();
}
catch
(
Exception
e
)
{
stream
=
null
;
return
"You failed to upload "
+
i
+
" => "
+
e
.
getMessage
();
}
}
else
{
return
"You failed to upload "
+
i
+
" because the file was empty."
;
}
}
return
"upload successful"
;
}
}
\ 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