Teilen Sie eine Datei hochladen und laden Sie das im Projekt verwendete Bild herunter und komprimieren Sie sie direkt aus dem Projekt extrahiert :)
Die Codekopie lautet wie folgt:
Paket com.eabax.plugin.yundada.utils;
Import Java.awt.image.BuffenedImage;
Import Java.io.file;
importieren java.io.ioException;
Java.Text.SimpledateFormat importieren;
import Java.util.date;
importieren java.util.list;
import Java.util.uuid;
import Javax.imageo.imageo;
importieren javax.servlet.http.httpServletRequest;
import net.coobird.Thumbnailator.Thumbnails;
import org.apache.commons.FileUpload.FileItem;
import org.apache.commons.FileUpload.disk.diskFileTemfactory;
import org.apache.commons.FileUpload.servlet.servletFileUpload;
import org.json.jsonObject;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import com.eavax.plugin.yundada.gacontext;
public class FileUploaddownloadilil {
private statische endgültige Logger log = loggerfactory.getLogger (FileUploaddownloadUtil.class);
/**
* Laden Sie Dateien auf den Server hoch
* @param Anfrage
* @param type
* @zurückkehren
* @throws Ausnahme
*/
öffentliches statisches String -Upload (httpServletRequest -Anforderung, String -Typ) löst Ausnahme {aus {
boolean ismultiPart = servletFileUpload.ismultipartContent (Anfrage);
String SaveFileName = null;
if (ismultipart) {
String SavePath = Request.GetSession (). GetServletContext ()
.GetRealPath ("/")
+ "/Upload/";
String temppath = request.getSession (). GetServletContext ()
.GetRealPath ("/")
+ "/hochladen/temp/";
Datei SaveFile = New Datei (SAREPADE);
Datei tempfile = new Datei (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.ParSequest (Anfrage);
für (FileItem Element: FileItems) {
if (item.isformfield ()) {
// Funname = item.getString ();
} anders {
// String Dateiname = item.getName ();
// String
// fix = Dateiname.substring (Dateiname.lastIndexof (".")+1);
String fix = Typ;
Datum nowdate = new Date ();
SimpledateFormat SDF = new SimpledateFormat (
"yyyymmddhhmms");
String Dateiname = sdf.format (nowdate);
Dateiname += System.currentTimemillis ();
Dateiname + = "." + Fix;
SaveFileName = "/Upload/" + Dateiname;
Datei Datei = neue Datei (SAVEPATH + Dateiname);
item.write (Datei);
}
}
}
return SaveFileName;
}
/**
* Laden Sie Avatar hoch
* @param Anfrage
* @param type
* @zurückkehren
* @throws Ausnahme
*/
öffentliche statische String -Uploadheadshow (httpServletRequest -Anforderung, Gacontext -Kontext, String -Typ) löst Ausnahme {aus {
boolean ismultiPart = servletFileUpload.ismultipartContent (Anfrage);
String SaveFileName = null;
String imagePath = "/Upload/Headshow/";
String x = request.getParameter ("Länge");
String y = request.getParameter ("breit");
if (ismultipart) {
String HeadshowServicePath = Request.GetSession (). GetServletContext ()
.GetRealPath ("/")
+ ImagePath;
Datum nowdate = new Date ();
SimpledateFormat SDF = new SimpledateFormat (
"yyyymmddhhmms");
String Dateiname = context.getUserName ()+SDF.Format (nowdate);
Datei HeadshowFile = neue Datei (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.ParSequest (Anfrage);
für (FileItem Element: FileItems) {
if (item.isformfield ()) {
// Funname = item.getString ();
} anders {
String fix = Typ;
Dateiname + = "." + Fix;
SaveFileName = ImagePath + Dateiname;
Datei Datei = neue Datei (HeadshowServicePath + Dateiname);
item.write (Datei);
}
}
// das Bild komprimieren
if (x! = null &&! "", Equals (x) && y!
SaveFileName = thumbnailatorImage (ImagePath, Dateiname, Typ, Integer.ParseInt (x), Integer.ParseInt (y));
}
}
return SaveFileName;
}
/**
* Bilder hochladen und teilen
* @param Anfrage
* @param type
* @zurückkehren
* @throws Ausnahme
*/
public static jsonObject uploadarticleImage (httpServletRequest -Anforderung, Gacontext -Kontext, String -Typ) löst eine Ausnahme {aus {
boolean ismultiPart = servletFileUpload.ismultipartContent (Anfrage);
JsonObject SaveFileName = new JSONObject ();
String imagePath = "";
String x = request.getParameter ("Länge");
String y = request.getParameter ("breit");
if ("4" .Equals (Typ)) {
// Teilen Sie den Image -Pfad hochladen
ImagePath = "/Upload/articleImage/";
} else if ("5" .Equals (Typ)) {
// LINK -Bildpfad hochladen
ImagePath = "/Upload/linkImage/";
} anders {
// Der Weg zum Hochladen von Avatar
ImagePath = "/Upload/Headshow/";
}
if (ismultipart) {
String HeadshowServicePath = Request.GetSession (). GetServletContext ()
.GetRealPath ("/")
+ ImagePath;
Datei HeadshowFile = neue Datei (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.ParSequest (Anfrage);
für (FileItem Element: FileItems) {
UUid uUid = uUid.randomuuid ();
String Dateiname = uUid.toString ();
if (item.isformfield ()) {
// Funname = item.getString ();
} anders {
String fix = Typ;
Dateiname + = "." + Fix;
SaveFileName.put (uUid.toString (), ImagePath + Dateiname);
Datei Datei = neue Datei (HeadshowServicePath + Dateiname);
item.write (Datei);
}
// das Bild komprimieren
if (x! = null &&! "", Equals (x) && y!
String thumbnailatorname = thumbnailatorImage (ImagePath, Dateiname, Typ, Integer.ParseInt (x), Integer.ParseInt (y));
SaveFileName.put ("thumbnailatorimage", thumbnailatorname);
}
}
}
return SaveFileName;
}
/**
* Laden Sie die Komprimierung hoch und speichern Sie Bilder
* @param OldsavePath Original -Dateipfad
* @Param OldFileName Original -Dateiname
* @param Fix -Dateityp
* @param x Breite für die Komprimierung erforderlich
* @param y Die für die Komprimierung erforderliche Länge
* @zurückkehren
* @throws ioException
*/
public static String thumbnailatorImage (String Oldsavepath, String Oldfilename, String Fix, int x, int y) löst ioException {aus {
// Vorschaubild liest und komprimiert Bilder
BufferedImage Wassermarkbuffenedimage = Miniatursänen.of (Oldsavepath+Oldfilename)
// Vorschaubildmethode, Komprimierungsbilder komprimieren
.Size (x, y)
// als gepufferte Objekt lesen
.AsbuffenedImage ();
// Schreiben Sie die Bilder in den Speicher in die angegebene Datei
String SavePath = OldSavePath+x+"-"+y+"/";
Datei SaveFile = New Datei (SAREPADE);
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 Dateiname = uUid.toString ();
Dateiname + = "." + Fix;
String SaveFileName = SavePath+Dateiname;
DateidateiOutput = neue Datei (SaveFileName);
ImageIo.Write (WatermarkbuffeedImage, Fix, FileOutput);
return SaveFileName;
}
/**
* Laden Sie komprimiert und speichern Sie Bilder
* @param OldsavePath Original -Dateipfad
* @Param OldFileName Original -Dateiname
* @param Fix -Dateityp
* @param x Breite für die Komprimierung erforderlich
* @param y Die für die Komprimierung erforderliche Länge
* @zurückkehren
* @throws ioException
*/
public static String downloadThumbnailatorImage (String ServicePath, String URI, int x, int y) löst ioException {aus
// Überprüfen Sie, ob das Bild existiert
String urisubpath = uri.substring (0, uri.lastindexof ("/")+1); // vor dem Dateinamen nach dem Server
String -Dateiname = uri.substring (Uri.lastindexof ("/")+1, uri.length ()); // Dateiname
String GetThumbnailatorPath = servicePath+urisubpath+x+"-"+y+"/";
String SaveFileName = GetThumbnailatorPath+Dateiname;
Datei downFilepath = neue Datei (GetThumbnailatorPath); // Komprimieren Sie den Ordner nach dem Komprimieren
Datei downfile = neue Datei (SaveFileName); // Die Datei danach komprimieren
if (downfilepath.isdirectory () && downfile.exists ()) {
return SaveFileName;
} anders {
// Vorschaubild liest und komprimiert Bilder
log.Error (ServicePath+URI);
BufferedImage WatermarkbuffenedImage = Miniatursails.of (ServicePath+URI)
// Vorschaubildmethode, Komprimierungsbilder komprimieren
.Size (x, y)
// als gepufferte Objekt lesen
.AsbuffenedImage ();
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);
DateidateiOutput = neue Datei (SaveFileName);
ImageIo.Write (WatermarkbuffeedImage, "JPG", FileOutput);
}
return SaveFileName;
}
}
Das obige Inhalt in diesem Artikel, ich hoffe, es wird für alle hilfreich sein.