Ⅰ java thumbnails壓縮圖片佔用內存過大
縮小即可。javathumbnails壓縮圖片佔用內存過大的解決辦法:
1、打開壓縮圖網站,點擊選擇圖片壓縮按鈕。
2、圖片上傳前網站默認的壓縮等級是70,圖片上傳後會自動按照當前設置的壓縮等級進行壓縮,如果壓縮後的大小不合要求,可再次設置圖片的寬高尺寸、壓縮等級等參數,寬高留空時默認按照原圖尺寸進行壓縮,壓縮等級越小則壓縮後的圖片體積越小,最後點擊開始壓縮按鈕重新壓縮。
3、圖片壓縮後,我們就能明顯的看到圖片壓縮後的體積縮小了很多,點擊「保存圖片」即可。
Ⅱ Java上傳pdf文件,只壓縮大小,不改變成.zip/.rar文件
Java上傳pdf文件,只壓縮大小,不改變成.zip/.rar文件
可以,壓縮只是一種演算法,什麼語言都可以,比如某種格式的文件中1001010(二進制)代表漢子的"中"字,那麼壓縮演算法就是在編碼不沖突的情況下可以改變編碼長度,比如壓縮之後中字變成1010,這樣就節省空間了,這是我隨便舉的例子,具體的對應演算法可以網上查
Ⅲ java如何實現把一個大圖片壓縮到指定大小的圖片且長寬比不變
java要實現把一個大圖片壓縮到指定大小的圖片且長寬比不變可以嘗試以下操作:
Ⅳ 圖片壓縮上傳
你闡述的意思是:「讓圖片在到達伺服器之前進行處理」嗎?
一般的圖片壓縮上傳的意思是:用戶選擇圖片後,上傳到伺服器進行壓縮暫存,判斷是否符合大小。符合的話進行下一步。不符合刪除圖片。
我一般都是採用php的或java的圖片壓縮類,在網上有好多,進行修改整合一下就可以了。
希望對你有所幫助! 共同進步。
Ⅳ 求java上傳圖片時,如何改變圖片內存大小不是改變長和寬,例如:圖片3M改成90KB 最好有源碼,謝謝!
import java.io.*;
import java.util.zip.*;
public class GZIPcompress {
public static void main(String[] args) {
try {
BufferedReader in =
new BufferedReader(
new FileReader(args[0])); //圖片文件地址
BufferedOutputStream out =
new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream("test.gz"))); //壓縮後的文件名
System.out.println("Writing file");
int c;
while((c = in.read()) != -1)
out.write(c);
in.close();
out.close();
System.out.println("Reading file");
BufferedReader in2 =
new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
new FileInputStream("test.gz"))));
String s;
while((s = in2.readLine()) != null)
System.out.println(s);
} catch(Exception e) {
e.printStackTrace();
}
}
}
試試這個。。。。。。。。。。。。
Ⅵ 求助java壓縮圖片存儲大小的方法
可以使用Draw這個類,通過改變像素來改變存儲大小,實例如下:
(StringsrcFilePath,StringdescFilePath)throwsIOException{
Filefile=null;
BufferedImagesrc=null;
FileOutputStreamout=null;
ImageWriterimgWrier;
ImageWriteParamimgWriteParams;
//指定寫圖片的方式為jpg
imgWrier=ImageIO.getImageWritersByFormatName("jpg").next();
imgWriteParams=newjavax.imageio.plugins.jpeg.JPEGImageWriteParam(
null);
//要使用壓縮,必須指定壓縮方式為MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT);
//這里指定壓縮的程度,參數qality是取值0~1范圍內,
imgWriteParams.setCompressionQuality((float)1);
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED);
ColorModelcolorModel=ImageIO.read(newFile(srcFilePath)).getColorModel();//ColorModel.getRGBdefault();
//指定壓縮時使用的色彩模式
//imgWriteParams.setDestinationType(newjavax.imageio.ImageTypeSpecifier(
//colorModel,colorModel.createCompatibleSampleModel(16,16)));
imgWriteParams.setDestinationType(newjavax.imageio.ImageTypeSpecifier(
colorModel,colorModel.createCompatibleSampleModel(16,16)));
try{
if(isBlank(srcFilePath)){
returnfalse;
}else{
file=newFile(srcFilePath);System.out.println(file.length());
src=ImageIO.read(file);
out=newFileOutputStream(descFilePath);
imgWrier.reset();
//必須先指定out值,才能調用write方法,ImageOutputStream可以通過任何
//OutputStream構造
imgWrier.setOutput(ImageIO.createImageOutputStream(out));
//調用write方法,就可以向輸入流寫圖片
imgWrier.write(null,newIIOImage(src,null,null),
imgWriteParams);
out.flush();
out.close();
}
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}
returntrue;
}
publicstaticbooleanisBlank(Stringstring){
if(string==null||string.length()==0||string.trim().equals("")){
returntrue;
}
returnfalse;
}
Ⅶ java,web項目我想上傳一個rar壓縮文件,裡面有個excel和圖片。
解壓縮這一步不說了,前面有人說過了。
你要找不同文件的時候,如果文件比較少,像你說的只有兩個文件,你就按擴展名去判斷匹配就可以了。excel找xls,圖片找jpg或者png。
如果文件比較多的時候,你可以隨rar包上傳一個properties文件或者xml文件,在裡面配一下各個文件的文件名以便解析使用。
Ⅷ 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 ssh實現微信端操作 有圖片上傳的地方 怎麼能在上傳到伺服器之前進行等比例壓縮
public class struts2UploadAction extends ActionSupport {
private File uploadFile;// 得到上傳的文件
private String uploadFileContentType;// 得到文件的類型
private String uploadFileFileName;// 得到文件的名稱
public String load() throws IOException {
String RealPath = ServletActionContext.getServletContext().getRealPath("/piction");
File file = new File(RealPath);
if(!file.exists()){
file.mkdirs();
}
FileUtils.File(uploadFile, new File(file,uploadFileFileName));
String path=RealPath+"/"+uploadFileFileName;
ServletActionContext.getRequest().setAttribute("realpath",path);
return "success";
}
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
}
Ⅹ 上傳圖片,實現圖片壓縮 java.lang.IllegalArgumentException: Can't load standard profile: GRAY.pf
servlet代碼:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class FileUpload extends HttpServlet {
//定義一個ServletConfig對象
private ServletConfig sc;
/**
* Constructor of the object.
*/
public FileUpload() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
response.setContentType("text/html;charset=utf8");
PrintWriter out = response.getWriter();
//新建一個SmartUpload對象,接收用戶上傳文件
SmartUpload su = new SmartUpload();
su.initialize(sc, request, response);
su.upload();
Files fs = su.getFiles();
File frist = fs.getFile(0);
String filename = frist.getFileName();
String sctm = System.currentTimeMillis()+"";
//獲得文件的擴展名
String ext = frist.getFileExt();
//把文件保存到伺服器硬碟(以系統毫秒數命名)
frist.saveAs("/"+sctm+"."+ext);
String username = su.getRequest().getParameter("username");
System.out.println(username);
out.flush();
out.close();
} catch (SmartUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void init(ServletConfig sc) throws ServletException {
// Put your code here
this.sc = sc;
}
}
jsp代碼:
<body>
<form action="fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="fileXXX" /><!-- 不懈這個name就接收不到,使用不到 -->
用戶名:<input type="text" name="username" />
<br/>
<input type="submit" />
</form>
</body>