java壓縮上傳圖片-ag真人国际官网
『壹』 java上傳圖片 生成縮略圖,如果上傳的圖片尺寸比較小就壓縮處理
//將圖按比例縮小。
public static bufferedimage resize(bufferedimage source, int targetw, int targeth) {
// targetw,targeth分別表示目標長和寬
int type = source.gettype();
bufferedimage target = null;
double sx = (double) targetw / source.getwidth();
double sy = (double) targeth / source.getheight();
//這里想實現在targetw,targeth范圍內實現等比縮放。如果不需要等比縮放
//則將下面的if else語句注釋即可
if(sx>sy)
{
sx = sy;
targetw = (int)(sx * source.getwidth());
}else{
sy = sx;
targeth = (int)(sy * source.getheight());
}
if (type == bufferedimage.type_custom) { //handmade
colormodel cm = source.getcolormodel();
writableraster raster = cm.(targetw, targeth);
boolean alphapremultiplied = cm.isalphapremultiplied();
target = new bufferedimage(cm, raster, alphapremultiplied, null);
} else
target = new bufferedimage(targetw, targeth, type);
graphics2d g = target.creategraphics();
//smoother than exlax:
g.setrenderinghint(renderinghints.key_rendering, renderinghints.value_render_quality );
g.drawrenderedimage(source, affinetransform.getscaleinstance(sx, sy));
g.dispose();
return target;
}
public static void saveimageasjpg (string fromfilestr,string savetofilestr,int width,int hight)
throws exception {
bufferedimage srcimage;
// string ex = fromfilestr.substring(fromfilestr.indexof("."),fromfilestr.length());
string imgtype = "jpeg";
if (fromfilestr.tolowercase().endswith(".png")) {
imgtype = "png";
}
// system.out.println(ex);
file savefile=new file(savetofilestr);
file fromfile=new file(fromfilestr);
srcimage = imageio.read(fromfile);
if(width > 0 || hight > 0)
{
srcimage = resize(srcimage, width, hight);
}
imageio.write(srcimage, imgtype, savefile);
}
public static void main (string argv[]) {
try{
//參數1(from),參數2(to),參數3(寬),參數4(高)
saveimageasjpg("c:\\documents and settings\\xugang\\桌面\\tmr-06.jpg",
"c:\\documents and settings\\xugang\\桌面\\2.jpg",
120,120);
} catch(exception e){
e.printstacktrace();
}
}
『貳』 搭建java環境如何解壓縮
具體解壓縮方法如下:
java壓縮解壓縮文件的方法有,第一中藉助javajdk自帶的zipoutputstream和zipinputstream。第二種,藉助第三方jar,例如apachecommonscompress和ant。
前提,需要將ant的ant、jar和ant-launcher、jar添加到classpath中。先創建一個expander類,該類繼承了ant的org、apache、tools、ant、taskdefs、expand類。
第二步:使用expander類。
『叄』 java,web項目我想上傳一個rar壓縮文件,裡面有個excel和圖片。
解壓縮這一步不說了,前面有人說過了。
你要找不同文件的時候,如果文件比較少,像你說的只有兩個文件,你就按擴展名去判斷匹配就可以了。excel找xls,圖片找jpg或者png。
如果文件比較多的時候,你可以隨rar包上傳一個properties文件或者xml文件,在裡面配一下各個文件的文件名以便解析使用。
『肆』 java 什麼演算法壓縮文件最小
有三種方式實現java壓縮:
1、jdk自帶的包java.util.zip.zipoutputstream,不足之處,文件(夾)名稱帶中文時,出現亂碼問題,實現代碼如下:
/**
* 功能:把 sourcedir 目錄下的所有文件進行 zip 格式的壓縮,保存為指定 zip 文件
* @param sourcedir 如果是目錄,eg:d:\\myeclipse\\first\\testfile,則壓縮目錄下所有文件;
* 如果是文件,eg:d:\\myeclipse\\first\\testfile\\aa.zip,則只壓縮本文件
* @param zipfile 最後壓縮的文件路徑和名稱,eg:d:\\myeclipse\\first\\testfile\\aa.zip
*/
public file dozip(string sourcedir, string zipfilepath) throws ioexception {
file file = new file(sourcedir);
file zipfile = new file(zipfilepath);
zipoutputstream zos = null;
try {
// 創建寫出流操作
outputstream os = new fileoutputstream(zipfile);
bufferedoutputstream bos = new bufferedoutputstream(os);
zos = new zipoutputstream(bos);
string basepath = null;
// 獲取目錄
if(file.isdirectory()) {
basepath = file.getpath();
}else {
basepath = file.getparent();
}
zipfile(file, basepath, zos);
}finally {
if(zos != null) {
zos.closeentry();
zos.close();
}
}
return zipfile;
}
/**
* @param source 源文件
* @param basepath
* @param zos
*/
private void zipfile(file source, string basepath, zipoutputstream zos)
throws ioexception {
file[] files = null;
if (source.isdirectory()) {
files = source.listfiles();
} else {
files = new file[1];
files[0] = source;
}
inputstream is = null;
string pathname;
byte[] buf = new byte[1024];
int length = 0;
try{
for(file file : files) {
if(file.isdirectory()) {
pathname = file.getpath().substring(basepath.length() 1) "/";
zos.putnextentry(new zipentry(pathname));
zipfile(file, basepath, zos);
}else {
pathname = file.getpath().substring(basepath.length() 1);
is = new fileinputstream(file);
bufferedinputstream bis = new bufferedinputstream(is);
zos.putnextentry(new zipentry(pathname));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
}
}
}finally {
if(is != null) {
is.close();
}
}
}
2、使用org.apache.tools.zip.zipoutputstream,代碼如下,
package net.szh.zip;
import java.io.bufferedinputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.util.zip.crc32;
import java.util.zip.checkedoutputstream;
import org.apache.tools.zip.zipentry;
import org.apache.tools.zip.zipoutputstream;
public class zipcompressor {
static final int buffer = 8192;
private file zipfile;
public zipcompressor(string pathname) {
zipfile = new file(pathname);
}
public void compress(string srcpathname) {
file file = new file(srcpathname);
if (!file.exists())
throw new runtimeexception(srcpathname "不存在!");
try {
fileoutputstream fileoutputstream = new fileoutputstream(zipfile);
checkedoutputstream cos = new checkedoutputstream(fileoutputstream,
new crc32());
zipoutputstream out = new zipoutputstream(cos);
string basedir = "";
compress(file, out, basedir);
out.close();
} catch (exception e) {
throw new runtimeexception(e);
}
}
private void compress(file file, zipoutputstream out, string basedir) {
/* 判斷是目錄還是文件 */
if (file.isdirectory()) {
system.out.println("壓縮:" basedir file.getname());
this.compressdirectory(file, out, basedir);
} else {
system.out.println("壓縮:" basedir file.getname());
this.compressfile(file, out, basedir);
}
}
/** 壓縮一個目錄 */
private void compressdirectory(file dir, zipoutputstream out, string basedir) {
if (!dir.exists())
return;
file[] files = dir.listfiles();
for (int i = 0; i < files.length; i ) {
/* 遞歸 */
compress(files[i], out, basedir dir.getname() "/");
}
}
/** 壓縮一個文件 */
private void compressfile(file file, zipoutputstream out, string basedir) {
if (!file.exists()) {
return;
}
try {
bufferedinputstream bis = new bufferedinputstream(
new fileinputstream(file));
zipentry entry = new zipentry(basedir file.getname());
out.putnextentry(entry);
int count;
byte data[] = new byte[buffer];
while ((count = bis.read(data, 0, buffer)) != -1) {
out.write(data, 0, count);
}
bis.close();
} catch (exception e) {
throw new runtimeexception(e);
}
}
}
3、可以用ant中的org.apache.tools.ant.taskdefs.zip來實現,更加簡單。
package net.szh.zip;
import java.io.file;
import org.apache.tools.ant.project;
import org.apache.tools.ant.taskdefs.zip;
import org.apache.tools.ant.types.fileset;
public class zipcompressorbyant {
private file zipfile;
public zipcompressorbyant(string pathname) {
zipfile = new file(pathname);
}
public void compress(string srcpathname) {
file srcdir = new file(srcpathname);
if (!srcdir.exists())
throw new runtimeexception(srcpathname "不存在!");
project prj = new project();
zip zip = new zip();
zip.setproject(prj);
zip.setdestfile(zipfile);
fileset fileset = new fileset();
fileset.setproject(prj);
fileset.setdir(srcdir);
//fileset.setincludes("**/*.java"); 包括哪些文件或文件夾 eg:zip.setincludes("*.java");
//fileset.setexcludes(...); 排除哪些文件或文件夾
zip.addfileset(fileset);
zip.execute();
}
}
測試一下
package net.szh.zip;
public class testzip {
public static void main(string[] args) {
zipcompressor zc = new zipcompressor("e:\\szhzip.zip");
zc.compress("e:\\test");
zipcompressorbyant zca = new zipcompressorbyant("e:\\szhzipant.zip");
zca.compress("e:\\test");
}
}