已解决问题
谷歌大漠3333用户在2018.06.12提交了关于“史铁生J**a中String类型转MultipartFile”的提问,欢迎大家涌跃发表自己的观点。目前共有1个回答,最后更新于2024-07-17T03:05:42。希望大家能够帮助她。详细问题描述及疑问:期待您的答案,当代劳模,所有人都应该向你学习 !
详细问题描述及疑问:期待您的答案,当代劳模,所有人都应该向你学习 !
packagecom.xiaojd.test;
importj**a.io.File;
importj**a.io.FileInputStream;
i
importj**a.
importorg.apa
importorg.apa
importorg.apache.commons.fileupload.dis
importo
importorg.sp
publicclas
publicstaticvoidmain(Strin
StringpicPath="D://pic//1
MultipartFilefile=getMulFileByPath(picP
try{
byte[]bytes=file.getBytes();
for(byteb:bytes){
System.
}
}catch(IOExceptione){
e.printStackTrace
}
//图片上传
picUpload(f
}
privatestaticvoidpicUpload(Mu
//图片上传
if(file!=**ll){//判
Stringpath=**ll;//文件路径
Str
Stringf
System.out.println("上传的文件原名称:"+fileName);
//判断文件类型
type
if(type!=**ll){//判断文件类型
if("GIF".equals(type.toUpperCase())||"PNG".equals(type.
//项目在容器中
//Stringre
//哥哥自己写的路径
StringrealPath="F://pic//";
//自定义的文件名称
StringtrueFileName=fileName;
//设置存放图片文件的路径
path=realPath+trueFileName;
System.out.println("存放图片文件的路径:"+path);
//转存文件到指定的路径
try{
file.transferTo(newFile(path));
}catch(Exceptione){
e.printStackTrace();
}
System.out.println("文件成功上传到指定目录下");
}else{
System.out.println("不是我们想要的文件类型,请按要求重新上传");
}
}else{
System.out.println("文件类型为空");
}
}
System.out.println("没有找到相对应的文件");
}
privatestaticMultipartFilegetMulFileByPath(StringpicPath){
FileItemfileItem=createFileItem(picPath);
MultipartFilemfile=newCommonsMultipartFile(fileItem);
returnmfile;
}
privatestaticFileItemcreateFileItem(StringfilePath)
{
FileItemFactoryfactory=newDiskFileItemFactory(16,**ll);
StringtextFieldName="textField";
int**m=filePath.lastIndexOf(".");
StringextFile=filePath.substring(**m);
FileItemitem=factory.createItem(textFieldName,"text/plain",true,
"MyFileName"+extFile);
Filenewfile=newFile(filePath);
intbytesRead=0;
byte[]buffer=newbyte[8192];
try
{
FileInputStreamfis=newFileInputStream(newfile);
OutputStreamos=item.getOutputStream();
while((bytesRead=***.read(buffer,0,8192))
!=-1)
{
os.write(buffer,0,bytesRead);
}
os.close();
fis.close();
}
catch(IOExceptione)
{
e.printStackTrace();
}
returnitem;
}
}