有点乱,我用JSP写的,是实现类似于网页另存为功能的
<%@ page 
language="java"
contentType="text/html;charset=GBK"
import="java.io.*,java.net.*,java.util.regex.*"
%>
<% 
response.setHeader("Pragma","No-cache"); 
response.setHeader("Cache-Control","no-cache"); 
response.setDateHeader("Expires", 0); 
%> <%!
//保存文件
public String saveFile(String saveFolder,URL myUrl,String mainFileName) throws Exception
{
//URL myUrl = new  URL(fullUrl);
URLConnection urlconn = myUrl.openConnection();
InputStream ips = urlconn.getInputStream();
//取得文件名及路径
String filePath = "/" + mainFileName + ".files/" + myUrl.getPath(); //System.out.println(filePath); filePath = filePath.replaceAll("\\\\","/");
filePath = filePath.replaceAll("/+","/"); String fileName = filePath.substring(filePath.lastIndexOf("/"));
if(fileName == null || fileName.trim().equals("") || fileName.indexOf(".")<0){
filePath = filePath + "/temp.htm";
}
filePath = filePath.replaceAll("/+","/"); //System.out.println(filePath);
String fileFolder = "";//
if(filePath.lastIndexOf("/")>0){
fileFolder = filePath.substring(0,filePath.lastIndexOf("/"));
} //System.out.println(saveFolder + "/" + fileFolder);
File FileFolder = new File(saveFolder + "/" + fileFolder);
if(!FileFolder.exists()){
boolean f = FileFolder.mkdirs();
//System.out.println(f);
} //System.out.println(saveFolder + "/" + filePath);
File target_file=new File(saveFolder + "/" + filePath);
if(!target_file.exists()){
target_file.createNewFile();
} FileOutputStream fos=new FileOutputStream(target_file); int bSize = 1024;
byte[] b = new byte[bSize];
int L = 0; while((L=ips.read(b,0,bSize))>=0){
if(L>=bSize) fos.write(b);
else{
for(int i=0;i<L;i++){
fos.write(b[i]);
}
}
}
ips.close();
fos.close(); //System.out.println(filePath); return filePath;
}public String saveFile(String saveFolder,URL myUrl,String text,String mainFileName) throws Exception
{
//取得文件名及路径
String filePath = "/" + myUrl.getPath(); filePath = filePath.replaceAll("\\\\","/");
filePath = filePath.replaceAll("/+","/");

String fileName = mainFileName;
if(fileName == null || fileName.trim().equals("")){
fileName = filePath.substring(filePath.lastIndexOf("/"));
if(fileName == null || fileName.trim().equals("") || fileName.indexOf(".")<0){
fileName = "/temp.htm";
} String tmpFileName = fileName.toUpperCase();
if(tmpFileName.endsWith(".HTM") || tmpFileName.endsWith(".HTML")){
}
else{
fileName = (fileName.indexOf(".")>=0)?fileName.substring(0,fileName.indexOf(".")) + ".htm":fileName + ".htm";
}
} File target_file=new File(saveFolder + "/" + fileName);
if(!target_file.exists()){
target_file.createNewFile();
} FileWriter fw=new FileWriter(target_file);
fw.write(text);
fw.close(); return filePath;
}
%><%
String spec = request.getParameter("url");
String saveFolder = request.getParameter("path");
String fileName = request.getParameter("fileName");if(spec == null || spec.trim().equals("") || saveFolder == null || saveFolder.trim().equals("") || fileName == null || fileName.trim().equals("")){
out.println("失败.");
return;
}fileName = fileName.replaceAll("\\\\","/");
fileName = fileName.replaceAll("/+","");
URL myUrl = new  URL(spec);
URLConnection urlconn = myUrl.openConnection();
InputStream ips = urlconn.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();int bSize = 1024;
byte[] b = new byte[bSize];
int L = 0;while((L=ips.read(b,0,bSize))>=0){
if(L>=bSize) bos.write(b);
else{
for(int i=0;i<L;i++){
bos.write(b[i]);
}
}
}String str = bos.toString();
bos.close();
ips.close();
str = str.replaceAll("\\\\","/");
//使用正则,读取出图片
String pattern = "(src\\s*)=\\s*[\\\"\\\'](.*?)[\\\"\\\']\\B"; //  (\w+)=\"(.*?)\"\B     
//System.out.println(pattern);
String input=str;        
Pattern patt = Pattern.compile(pattern,Pattern.CASE_INSENSITIVE);
Matcher matcher = patt.matcher(input);
while (matcher.find()){
String src = matcher.group(2); URL tmpUrl = new URL(myUrl,src);
String tmpfile = "";
try{
tmpfile = saveFile(saveFolder,tmpUrl,fileName);
}
catch(Exception ex){
continue;
} tmpfile = tmpfile.replaceAll("\\\\","/");
tmpfile = tmpfile.replaceAll("/+","/");

tmpfile = (tmpfile.startsWith("/"))?tmpfile.substring(1):tmpfile; //System.out.println(src);
//System.out.println(tmpfile); src = src.replace('.','\'');
src = src.replaceAll("\'","\\\\.");

//System.out.println(src);
//System.out.println("......................."); str = str.replaceAll("=\\s*[\\\"\\\']\\s*"+src+"\\s*[\\\"\\\']","=\""+tmpfile+"\"");
}//使用正则处理样式表
String pattern2 = "(<link.*href\\s*)=\\s*[\\\"\\\'](.*?)[\\\"\\\']\\B";//<link href="
Pattern patt2 = Pattern.compile(pattern2,Pattern.CASE_INSENSITIVE);
Matcher matcher2 = patt2.matcher(input);
while (matcher2.find()){
String src = matcher2.group(2); URL tmpUrl = new URL(myUrl,src);
String tmpfile = "";
try{
tmpfile = saveFile(saveFolder,tmpUrl,fileName);
}
catch(Exception ex){
continue;
} tmpfile = tmpfile.replaceAll("\\\\","/");
tmpfile = tmpfile.replaceAll("/+","/");

tmpfile = (tmpfile.startsWith("/"))?tmpfile.substring(1):tmpfile; src = src.replace('.','\'');
src = src.replaceAll("\'","\\\\.");
str = str.replaceAll("=\\s*[\\\"\\\']\\s*"+src+"\\s*[\\\"\\\']","=\""+tmpfile+"\"");
}saveFile(saveFolder,myUrl,str,fileName);
%>

解决方案 »

  1.   

    谢谢,不过我更想要java 的代码。
      

  2.   

    现在的关键问题是怎样从URI标识的资源中读数据:
    举例:
    String tmp="Http://localhost/img/xxx.jpg";
    RandomAccessFile rSrc=new RandomAccessFile(tmp,"r");
    代码在编译时没有问题,执行时会报错:
    (文件名、目录名或卷标语法不正确)。
    这时从错误信息中看到java把路径变成了“http:\localhost\img\xxx.jpg”当然不对了,请问怎么解决这个问题。
      

  3.   

    to  easydozer:
    这个问题已经在另一个帖子中解决了。不过还是感谢你的认真。分都给你了。