如何找到含有空格的文件夹名下的文件 <a href="C:/Program Files/Apache Software Foundation/Tomcat 5.0/webapps/aa.jpg">含有空格的文件</a>你是不要这样的.就是把路径用引号引起来就可以了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是读取网站的图片好办,比如你放在Tomcat 5.0/webapps/image/a.jpg,那么就直接/image/a.jpg(image前面要不要那个斜杠记不清了),如果想获得绝对路径,可以使用request.getServletPath("/");获得相对路径request.getRealPath("/");获得绝对路径 String.replaceAll(" ",);把空格替换掉就行了 哦,不好意思,没说清楚我找了个类(不是自己写的),目的是:将超过指定大小的图片按比例缩小到指定规格,保存为另外的图片,代码如下:/** * */package com.bin.Test;import java.awt.Graphics2D;import java.awt.RenderingHints;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import java.awt.image.ColorModel;import java.awt.image.WritableRaster;import java.io.File;import javax.imageio.ImageIO;/** * @author Administrator * */public class ControlImage { public static BufferedImage resize(BufferedImage source) { int type = source.getType(); BufferedImage target = null; double width = source.getWidth(); double height = source.getHeight(); double wi = 0.0, he = 0.0, hh = 0.0; int w = 0, h = 0; if (width > 94) { wi = 94; hh = source.getHeight(); if (hh > 120) { he = 120; wi = (120 * height) / hh; System.out.println(wi + "----" + he); } } else if (height > 120 && width < 94) { wi = width; he = 120; } w = (int) wi; h = (int) he; System.out.println(w + "----" + h); if (type == BufferedImage.TYPE_CUSTOM) { ColorModel cm = source.getColorModel(); WritableRaster raster = cm.createCompatibleWritableRaster(w, h); boolean bl = cm.isAlphaPremultiplied(); target = new BufferedImage(cm, raster, bl, null); } else { target = new BufferedImage(w, h, type); } Graphics2D g = target.createGraphics(); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.drawRenderedImage(source, AffineTransform.getScaleInstance(w, h)); g.dispose(); return target; } public static void saveImageAsJpg(String fromFileStr, String saveToFileStr) 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); srcImage = ControlImage.resize(srcImage); ImageIO.write(srcImage, imgType, saveFile); } public static void main(String argv[]) { try { // 参数1(from),参数2(to),参数3(宽),参数4(高) ControlImage.saveImageAsJpg("F:\\rb\\images\\m_2.jpg", "f:/7.jpg"); System.out.println("ok----"); } catch (Exception e) { e.printStackTrace(); } }}main方法中有个ControlImage.saveImageAsJpg()方法,第一个和第二个参数是图片的源路径+文件和新路径+文件,但是对于路径中含有空格的处理不了就这了,可能是我给的那个路径误导了大家,呵呵 windows下会有带空格的目录,linux下没有,但是linux下的路径应该是怎样的?(还没接触过linux,不会搞,-_-!) 根据经验,java的代码不要放在有空格的或者含有中文的文件夹下,容易有问题。在处理文件路径的时候可以采用这种方式:可以根据不同操作系统产生对应的"/"or"\"代码如下: File fDir=new File(File.separator); String strFile="JavaLesson"+File.separator+"Lesson6"; File f=new File(fDir,strFile); 我的java代码是放在没有空格的而且是英文的文件夹下的但是我如果从含有空格的目录下 取得 图片路径+图片名的话怎么办,当然把那个图片放到另外的无空格的’而且英文的文件夹下是可以,但是遇到了有空格的怎么处理呢? 怎么我没问题呀,ControlImage.saveImageAsJpg("d:\\my temp\\a.jpg", "d:/a.jpg");我直接试了下,没问题的 不行啊报如下异常:javax.imageio.IIOException: Can't read input file! at javax.imageio.ImageIO.read(Unknown Source) at com.bin.Demo.ControlImage.saveImageAsJpg(ControlImage.java:76) at com.bin.Demo.ControlImage.main(ControlImage.java:86)76行是: srcImage = ImageIO.read(fromFile);86行是: ControlImage.saveImageAsJpg("F:\\aa aa\\bb bb\\7.jpg", "f:/1.jpg");我用不含空格的路径就是好的所以这肯定是因为空格问题啦! 呵呵,如果還沒解決,可試一下以下這種方法!ControlImage.saveImageAsJpg("\"" + "d:\\my temp\\a.jpg" + "\"", "d:/a.jpg"); 我做了一个论坛 别人输入标题的时候带了javascript代码 然后我就点击不进去了 spring 2.5的问题 顺便散分 spring 找不到包含ActionSupport包 Struts上传文件的时间统计问题 请您为高中毕业生指出另外一条康庄大道! 问一个hibernate里的,关于复合主键的问题 问一个涉及<html:select/> <html:options/><html:option/>的问题 jasperreports 求助 tomcat平台下运行出了一个小错,大家帮帮我哦 大家所知道的软件公司用什么开发工具 ireport中的模糊查询 Ireport 实现子表的动态列问题
request.getRealPath("/");获得绝对路径
我找了个类(不是自己写的),目的是:将超过指定大小的图片按比例缩小到指定规格,保存为另外的图片,代码如下:
/**
*
*/
package com.bin.Test;import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;import javax.imageio.ImageIO;/**
* @author Administrator
*
*/
public class ControlImage {
public static BufferedImage resize(BufferedImage source) {
int type = source.getType();
BufferedImage target = null; double width = source.getWidth();
double height = source.getHeight();
double wi = 0.0, he = 0.0, hh = 0.0;
int w = 0, h = 0; if (width > 94) {
wi = 94;
hh = source.getHeight();
if (hh > 120) {
he = 120;
wi = (120 * height) / hh;
System.out.println(wi + "----" + he);
}
} else if (height > 120 && width < 94) {
wi = width;
he = 120;
}
w = (int) wi;
h = (int) he;
System.out.println(w + "----" + h); if (type == BufferedImage.TYPE_CUSTOM) {
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(w, h);
boolean bl = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, bl, null);
} else {
target = new BufferedImage(w, h, type);
}
Graphics2D g = target.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(w, h));
g.dispose(); return target;
} public static void saveImageAsJpg(String fromFileStr, String saveToFileStr)
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); srcImage = ControlImage.resize(srcImage); ImageIO.write(srcImage, imgType, saveFile);
} public static void main(String argv[]) {
try {
// 参数1(from),参数2(to),参数3(宽),参数4(高)
ControlImage.saveImageAsJpg("F:\\rb\\images\\m_2.jpg", "f:/7.jpg");
System.out.println("ok----");
} catch (Exception e) {
e.printStackTrace();
}
}
}
main方法中有个ControlImage.saveImageAsJpg()方法,第一个和第二个参数是图片的源路径+文件和新路径+文件,但是对于路径中含有空格的处理不了就这了,可能是我给的那个路径误导了大家,呵呵
在处理文件路径的时候可以采用这种方式:可以根据不同操作系统产生对应的"/"or"\"代码如下:
File fDir=new File(File.separator);
String strFile="JavaLesson"+File.separator+"Lesson6";
File f=new File(fDir,strFile);
ControlImage.saveImageAsJpg("d:\\my temp\\a.jpg", "d:/a.jpg");
我直接试了下,没问题的
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at com.bin.Demo.ControlImage.saveImageAsJpg(ControlImage.java:76)
at com.bin.Demo.ControlImage.main(ControlImage.java:86)76行是: srcImage = ImageIO.read(fromFile);
86行是: ControlImage.saveImageAsJpg("F:\\aa aa\\bb bb\\7.jpg", "f:/1.jpg");我用不含空格的路径就是好的所以这肯定是因为空格问题啦!
ControlImage.saveImageAsJpg("\"" + "d:\\my temp\\a.jpg" + "\"", "d:/a.jpg");