当前位置:首页 » 文件传输 » java图片压缩后上传
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

java图片压缩后上传

发布时间: 2023-01-15 10:09:22

Ⅰ 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>