<span style=
"font-size:14px;"
>UploadMediaService :
public static String uploadLocalMedia(String path,HttpServletRequest request){
String filename =
""
;
DiskFileItemFactory factory =
new
DiskFileItemFactory();
factory.setRepository(
new
File(path));
factory.setSizeThreshold(1024*1024) ;
ServletFileUpload upload =
new
ServletFileUpload(factory);
try
{
List<FileItem> list = upload.parseRequest(request);
for
(FileItem item : list) {
if
(item.isFormField()) {
String value = item.getString() ;
}
else
{
String value = item.getName() ;
int start = value.lastIndexOf(
"\\"
);
filename = value.substring(start+1);
System.out.println(
"filename="
+ filename);
OutputStream out =
new
FileOutputStream(
new
File(path,filename));
InputStream
in
= item.getInputStream() ;
int length = 0 ;
byte [] buf =
new
byte[1024] ;
while
( (length =
in
.read(buf) ) != -1) {
out.write(buf, 0, length);
}
in
.close();
out.close();
}
}
}
catch
(FileUploadException e) {
log.error(
"文件上传异常:"
,e);
}
catch
(Exception e) {
log.error(
"文件处理IO异常:"
,e);
}
return
filename ;
}
</span>
网友评论