package
;
import
.*;
import
;
import
;
import
;
import
;
import
;
import
;
import
;
import
;
import
;
import
;
import
;
import
;
import
;
@Controller
@RequestMapping
(value = UploadURL.MODE_NAME)
public
class
UploadController
extends
BaseController {
@RequestMapping
(value = UploadURL.IMAGE_UPLOAD)
@ResponseBody
public
String uploadFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response)
throws
IllegalArgumentException, Exception {
// 参数列表
Map<String, Object> map =
new
HashMap<>();
(
"file"
, file);
savePic((), ());
//请求接口
String apiReturnStr =
""
;
//apiHttpRequest(map, API_HOST_URL + "/image/upload");
return
apiReturnStr;
}
private
void
savePic(InputStream inputStream, String fileName) {
OutputStream os =
null
;
try
{
String path =
"D:\\testFile\\"
;
// 2、保存到临时文件
// 1K的数据缓冲
byte
[] bs =
new
byte
[
1024
];
// 读取到的数据长度
int
len;
// 输出的文件流保存到本地文件
File tempFile =
new
File(path);
if
(!()) {
();
}
os =
new
FileOutputStream(() + + fileName);
// 开始读取
while
((len = (bs)) != -
1
) {
(bs,
0
, len);
}
}
catch
(IOException e) {
();
}
catch
(Exception e) {
();
}
finally
{
// 完毕,关闭所有链接
try
{
();
();
}
catch
(IOException e) {
();
}
}
}
/**
* <p class="detail">
* 功能:公共Action
* </p>
*
* @date 2016年9月8日
* @author wangsheng
*/
private
String allowSuffix =
"jpg,png,gif,jpeg"
;
//允许文件格式
private
long
allowSize = 2L;
//允许文件大小
private
String fileName;
private
String[] fileNames;
public
String getAllowSuffix() {
return
allowSuffix;
}
public
void
setAllowSuffix(String allowSuffix) {
this
.allowSuffix = allowSuffix;
}
public
long
getAllowSize() {
return
allowSize *
1024
*
1024
;
}
public
void
setAllowSize(
long
allowSize) {
this
.allowSize = allowSize;
}
public
String getFileName() {
return
fileName;
}
public
void
setFileName(String fileName) {
this
.fileName = fileName;
}
public
String[] getFileNames() {
return
fileNames;
}
public
void
setFileNames(String[] fileNames) {
this
.fileNames = fileNames;
}
/**
* <p class="detail">
* 功能:重新命名文件
* </p>
*
* @return
* @author wangsheng
* @date 2016年9月8日
*/
private
String getFileNameNew() {
SimpleDateFormat fmt =
new
SimpleDateFormat(
"yyyyMMddHHmmssSSS"
);
return
(
new
Date());
}
/**
* <p class="detail">
* 功能:文件上传
* </p>
*
* @param files
* @param destDir
* @throws Exception
* @author wangsheng
* @date 2014年9月25日
*/
public
void
uploads(MultipartFile[] files, String destDir, HttpServletRequest request)
throws
Exception {
String path = ();
String basePath = () +
"://"
+ () +
":"
+ () + path;
try
{
fileNames =
new
String[];
int
index =
0
;
for
(MultipartFile file : files) {
String suffix = ().substring(().lastIndexOf(
"."
) +
1
);
int
length = getAllowSuffix().indexOf(suffix);
if
(length == -
1
) {
throw
new
Exception(
"请上传允许格式的文件"
);
}
if
(() > getAllowSize()) {
throw
new
Exception(
"您上传的文件大小已经超出范围"
);
}
String realPath = ().getServletContext().getRealPath(
"/"
);
File destFile =
new
File(realPath + destDir);
if
(!()) {
();
}
String fileNameNew = getFileNameNew() +
"."
+ suffix;
//
File f =
new
File(() +
"\\"
+ fileNameNew);
(f);
();
fileNames[index++] = basePath + destDir + fileNameNew;
}
}
catch
(Exception e) {
throw
e;
}
}
/**
*功能:文件上传
*
* @param file
* @param destDir
* @throws Exception
* @author wangsheng
* @date 2016年9月8日
*/
public
void
upload(MultipartFile file, String destDir, HttpServletRequest request)
throws
Exception {
String path = ();
String basePath = () +
"://"
+ () +
":"
+ () + path;
try
{
String suffix = ().substring(().lastIndexOf(
"."
) +
1
);
int
length = getAllowSuffix().indexOf(suffix);
if
(length == -
1
) {
throw
new
Exception(
"请上传允许格式的文件"
);
}
if
(() > getAllowSize()) {
throw
new
Exception(
"您上传的文件大小已经超出范围"
);
}
String realPath = ().getServletContext().getRealPath(
"/"
);
File destFile =
new
File(realPath + destDir);
if
(!()) {
();
}
String fileNameNew = getFileNameNew() +
"."
+ suffix;
File f =
new
File(() +
"/"
+ fileNameNew);
(f);
();
fileName = basePath + destDir + fileNameNew;
}
catch
(Exception e) {
throw
e;
}
}
}