ファイルのアップロードを共有して、プロジェクトで使用されている画像をダウンロードして圧縮し、プロジェクトから直接抽出しました:)
コードコピーは次のとおりです。
パッケージcom.eabax.plugin.yundada.utils;
java.awt.image.bufferedimageをインポートします。
java.io.fileをインポートします。
java.io.ioexceptionをインポートします。
java.text.simpledateformatをインポートします。
Import Java.util.date;
java.util.listをインポートします。
java.util.uuidをインポートします。
javax.imageio.imageioをインポートします。
javax.servlet.http.httpservletrequestをインポートします。
net.coobird.thumbnailator.thumbnailsをインポートします。
org.apache.commons.fileupload.fileitemをインポートします。
org.apache.commons.fileupload.disk.diskfileitemfactoryをインポートします。
Import org.apache.commons.fileupload.servlet.servletfileupload;
Import org.json.jsonobject;
org.slf4j.loggerをインポートします。
org.slf4j.loggeractoryをインポートします。
com.eabax.plugin.yundada.gacontextをインポートします。
public class fileuploaddownloadutil {
private static final logger log = loggerfactory.getLogger(fileuploaddownloadutil.class);
/**
*ファイルをサーバーにアップロードします
* @paramリクエスト
* @paramタイプ
* @戻る
* @Throws例外
*/
public static string upload(httpservletrequest request、string type)スロー例外{
boolean ismultipart = servletfileupload.ismultipartcontent(request);
文字列savefileName = null;
if(ismultipart){
文字列savepath = request.getSession()。getServletContext()
.getRealPath( "/")
+ "/upload/";
string temppath = request.getSession()。getServletContext()
.getRealPath( "/")
+ "/upload/temp/";
ファイルSaveFile = new File(SavePath);
ファイルtempfile = new file(temppath);
if(!savefile.isdirectory())
savefile.mkdirs();
if(!tempfile.isdirectory())
tempfile.mkdirs();
DiskFileItemFactory Factory = new DiskFileItemFactory();
Factory.SetsizethReshold(1024 * 4);
Factory.setRepository(Tempfile);
servletfileupload uploader = new servletfileupload(Factory);
uploader.setsizemax(20 * 1024 * 1024);
List <FileItem> fileItems = uploader.parserequest(request);
for(fileitem item:fileitems){
if(item.isformfield()){
// funname = item.getString();
} それ以外 {
//文字列filename = item.getName();
// 弦
// fix = filename.substring(filename.lastindexof( "。")+1);
文字列fix = type;
date nowdate = new date();
SimpleDateFormat sdf = new SimpleDateFormat(
「yyyymmddhhmmss」);
文字列filename = sdf.format(nowdate);
filename += system.currenttimemillis();
filename + = "。 +
savefileName = "/upload/" + filename;
ファイルfile = new File(SavePath + Filename);
item.write(file);
}
}
}
savefileNameを返します。
}
/**
*アバターをアップロードします
* @paramリクエスト
* @paramタイプ
* @戻る
* @Throws例外
*/
public static string uploadheadshow(httpservletrequestリクエスト、gacontextコンテキスト、文字列タイプ)スロー例外{
boolean ismultipart = servletfileupload.ismultipartcontent(request);
文字列savefileName = null;
文字列imagepath = "/upload/headshow/";
文字列x = request.getParameter( "length");
文字列y = request.getParameter( "wide");
if(ismultipart){
string headshowservicepath = request.getSession()。getServletContext()
.getRealPath( "/")
+ ImagePath;
date nowdate = new date();
SimpleDateFormat sdf = new SimpleDateFormat(
「yyyymmddhhmmss」);
string filename = context.getusername()+sdf.format(nowdate);
ファイルheadshowfile = new file(headshowservicepath);
if(!headshowfile.isdirectory())
headshowfile.mkdirs();
DiskFileItemFactory Factory = new DiskFileItemFactory();
Factory.SetsizethReshold(1024 * 4);
Factory.setRepository(headshowfile);
servletfileupload uploader = new servletfileupload(Factory);
uploader.setsizemax(20 * 1024 * 1024);
List <FileItem> fileItems = uploader.parserequest(request);
for(fileitem item:fileitems){
if(item.isformfield()){
// funname = item.getString();
} それ以外 {
文字列fix = type;
filename + = "。 +
SaveFileName = ImagePath + Filename;
ファイルfile = new file(headshowservicepath + filename);
item.write(file);
}
}
//写真を圧縮します
if(x!= null &&! ""、。equals(x)&& y!= null &&! ""、。equals(y)){
savefileName = thumbnailatorimage(imagepath、filename、type、integer.parseint(x)、integer.parseint(y));
}
}
savefileNameを返します。
}
/**
*写真をアップロードして共有します
* @paramリクエスト
* @paramタイプ
* @戻る
* @Throws例外
*/
public static jsonObject uploadarticleImage(httpservletrequest request、gacontextコンテキスト、文字列タイプ)スロー例外{
boolean ismultipart = servletfileupload.ismultipartcontent(request);
jsonobject savefilename = new jsonobject();
文字列imagepath = "";
文字列x = request.getParameter( "length");
文字列y = request.getParameter( "wide");
if( "4" .equals(type)){
//アップロード画像パスを共有します
ImagePath = "/upload/articleimage/";
} else if( "5" .equals(type)){
// Image Pathのアップロードをリンクします
ImagePath = "/upload/linkimage/";
} それ以外 {
//アバターをアップロードするパス
ImagePath = "/upload/headshow/";
}
if(ismultipart){
string headshowservicepath = request.getSession()。getServletContext()
.getRealPath( "/")
+ ImagePath;
ファイルheadshowfile = new file(headshowservicepath);
if(!headshowfile.isdirectory())
headshowfile.mkdirs();
DiskFileItemFactory Factory = new DiskFileItemFactory();
Factory.SetsizethReshold(1024 * 4);
Factory.setRepository(headshowfile);
servletfileupload uploader = new servletfileupload(Factory);
uploader.setsizemax(20 * 1024 * 1024);
List <FileItem> fileItems = uploader.parserequest(request);
for(fileitem item:fileitems){
uuid uuid = uuid.randomuuid();
string filename = uuid.tostring();
if(item.isformfield()){
// funname = item.getString();
} それ以外 {
文字列fix = type;
filename + = "。 +
savefilename.put(uuid.tostring()、imagepath + filename);
ファイルfile = new file(headshowservicepath + filename);
item.write(file);
}
//写真を圧縮します
if(x!= null &&! ""、。equals(x)&& y!= null &&! ""、。equals(y)){
string thumbnailatorname = thumbnailatorimage(imagepath、filename、type、integer.parseint(x)、integer.parseint(y));
savefilename.put( "thumbnailatorimage"、thumbnailatorname);
}
}
}
savefileNameを返します。
}
/**
*圧縮をアップロードし、写真を保存します
* @Param OldSavePathオリジナルファイルパス
* @Param OldFileName元のファイル名
* @Param Fileタイプ
* @param x圧縮に必要な幅
* @param y圧縮に必要な長さ
* @戻る
* @throws ioexception
*/
public static string thumbnailatorimage(string oldsavepath、string oldfilename、string fix、int x、int y)throws ioexception {
//サムネイルは写真を読み取り、圧縮します
bufferedimage watermarkbufferedimage = thumbnails.of(oldsavepath+oldfilename)
//サムネイル法、写真を圧縮します
.size(x、y)
// bufferedimageオブジェクトとして読み取ります
.asbufferedimage();
//指定されたファイルにメモリ内の写真を書き込みます
文字列savepath = oldsavepath+x+" - "+y+"/";
ファイルSaveFile = new File(SavePath);
if(!savefile.isdirectory())
savefile.mkdirs();
DiskFileItemFactory Factory = new DiskFileItemFactory();
Factory.SetsizethReshold(1024 * 4);
Factory.setRepository(SaveFile);
servletfileupload uploader = new servletfileupload(Factory);
uploader.setsizemax(20 * 1024 * 1024);
uuid uuid = uuid.randomuuid();
string filename = uuid.tostring();
filename + = "。 +
文字列savefileName = savepath+filename;
file fileoutput = new file(savefilename);
Imageio.write(watermarkbufferedimage、fix、fileoutput);
savefileNameを返します。
}
/**
*圧縮された写真をダウンロードして写真を保存します
* @Param OldSavePathオリジナルファイルパス
* @Param OldFileName元のファイル名
* @Param Fileタイプ
* @param x圧縮に必要な幅
* @param y圧縮に必要な長さ
* @戻る
* @throws ioexception
*/
public static string downloadthumbnailatorimage(string servicepath、string uri、int x、int y)throws ioexception {
//画像が存在するかどうかを確認します
stringurisubpath = uri.substring(0、uri.lastindexof( "/")+1); //サーバーの後にファイル名の前に
string filename = uri.substring(uri.lastindexof( "/")+1、uri.length()); //ファイル名
String getThumbnailatorPath = servicepath+urisubpath+x+" - "+y+"/";
文字列savefileName = getThumbnailatorPath+filename;
ファイルdownfilepath = new File(getThumbnailatorPath); //圧縮後にフォルダーを圧縮します
ファイルdownfile = newファイル(savefilename); //ファイルをその後圧縮します
if(downfilepath.isdirectory()&& downfile.exists()){
savefileNameを返します。
} それ以外 {
//サムネイルは写真を読み取り、圧縮します
log.error(servicepath+uri);
bufferedimage watermarkbufferedimage = thumbnails.of(servicepath+uri)
//サムネイル法、写真を圧縮します
.size(x、y)
// bufferedimageオブジェクトとして読み取ります
.asbufferedimage();
if(!downfilepath.isdirectory()){
downfilepath.mkdirs();
}
DiskFileItemFactory Factory = new DiskFileItemFactory();
Factory.SetsizethReshold(1024 * 4);
Factory.setrepository(downfilepath);
servletfileupload uploader = new servletfileupload(Factory);
uploader.setsizemax(20 * 1024 * 1024);
file fileoutput = new file(savefilename);
Imageio.write(watermarkbufferedimage、 "jpg"、fileoutput);
}
savefileNameを返します。
}
}
上記は、この記事で共有されているすべてのコンテンツです。すべての人に役立つことを願っています。