从本机的文件下载方法
package com.xdja.web.file;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;import com.xdja.web.Hnipms;
import java.util.*;
import java.io.*;
public class FileDownload implements Controller{
private Hnipms hnipms;

public ModelAndView handleRequest(HttpServletRequest request,
            HttpServletResponse response)
throws Exception {

Test a = new Test();
String str=request.getSession().getServletContext().getRealPath("");
String uploadDir =str+"/UserFiles";
a.refreshFileList(uploadDir);

List list = a.getFileList(); request.setAttribute("list",list);
Map myMap = new HashMap();
      myMap.put("tpxw", hnipms.getXwfenlei("1","5"));
return new ModelAndView("file/filedownload",myMap);
} public Hnipms getHnipms() {
return hnipms;
} public void setHnipms(Hnipms hnipms) {
this.hnipms = hnipms;
}}
class Test
{ private ArrayList filelist = new ArrayList();  public void refreshFileList(String strPath) { 
       File dir = new File(strPath); 
       File[] files = dir.listFiles();         if (files == null) 
               return;         for (int i = 0; i < files.length; i++) { 
               if (files[i].isDirectory()) { 
                       refreshFileList(files[i].getAbsolutePath());
                       String str=files[i].getAbsolutePath();
                       String stradd=str.substring(33, str.length());
                       filelist.add(stradd); 
//System.out.println( files[i].getAbsolutePath() );  
               } else { 
                //filelist.add(files[i].getAbsolutePath());  
                String str=files[i].getAbsolutePath();
                       String stradd=str.substring(33, str.length());
                       filelist.add(stradd); 
                       String strFileName = files[i].getAbsolutePath().toLowerCase(); 
//System.out.println( files[i].getAbsolutePath() );                  
               } 
       } 
}

public ArrayList getFileList()
{
return this.filelist;
}
}
 这是对FileDownload.java 文件中所用到的绝对路径的解释,以便日后所下载文件改变路径后的处理
 
  String str=files[i].getAbsolutePath();
                       String stradd=str.substring(37, str.length());
                       filelist.add(stradd); 
                       String strFileName = files[i].getAbsolutePath().toLowerCase(); 
                       
                       
             其中getAbsolutePath();获得的是相对本机的绝对路径;
             String stradd=str.substring(37, str.length());
             上句是根据本机所要下的目标文件的路径的截取,要根据文件的不同深度进行修改;
             
             所对应的JSP文件的
             <a href="/UserFiles/<c:out value="${m}"/>" target="_blank"><font color="#345643"><u><c:out value="${m}"/><br></u></font></a>
             也要进行相应的修改 图片轮换
!--begin:图片轮换开始-->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ page contentType="text/html; charset=gb2312" pageEncoding="GBK"%>
<script language=JavaScript>
var imgUrl=new Array();
var imgLink=new Array();
var imgTz=new Array();
var buttonShow=1;
var buttonPos=1;
var buttonX;
var buttonY;
var bakularCode=""
var adNum=0;
var dakularCode="";
<%
int c=0;
%>
<c:forEach var="m" begin="0" end="2" items="${tpxw}">
<%c=c+1;%>
//var str="${fn:substring(m.uploadfile,7,31)}";
var str="${m.uploadfile}";
//为求的图片的文件长度做准备
imgUrl[<%=c%>]="upload/${fn:substring(m.uploadfile,7,str.length+31)}";
//由于此网页不支持"\"所以截取只截取了图片的文件名,然后加上固定的"upload/"文件夹 即可在主页面上显示图片轮换
//alert(str);
// imgUrl[<%=c%>]="upload/displaypic.jpg";
imgLink[<%=c%>]="shownews_5detail.htm?id=<c:out value="${m.id}"/>";
imgTz[<%=c%>]="${fn:substring(m.bt,0,18)}...";
</c:forEach>
var imgPre=new Array();
for (i=1;i<8;i++)
{
imgPre[i]=new Image();
imgPre[i].src=imgUrl[i];
}
function setTransition()
{
if (document.all)
{
imgUrlrotator.filters.revealTrans.Transition=23;//Math.floor(Math.random()*23)
imgUrlrotator.filters.revealTrans.apply();
}
}
function playTransition()
{
if (document.all)
imgUrlrotator.filters.revealTrans.play()
}
function nextAd(toNum)
{
if(adNum<imgUrl.length-1)
adNum++ ;
else
adNum=1;
if(toNum!=null) adNum=toNum;
setTransition();
document.images.imgUrlrotator.src=imgUrl[adNum];
document.getElementById('linktext').innerHTML=imgTz[adNum];
document.images.imgUrlrotator.alt=''+imgTz[adNum];
document.getElementById('linktext').innerHTML="<a href="+imgLink[adNum]+" target=_blank class=px14-lh20>"+imgTz[adNum]+"</a>";
playTransition();
checkButton();
if(toNum!=null)
{toNum=null;theTimer=setTimeout("nextAd()", 7000);}
else
{theTimer=setTimeout("nextAd()", 7000);}
}
function jump2url()
{
jumpUrl=imgLink[adNum];jumpTarget='_blank';
if (jumpUrl != '')
{  if (jumpTarget != '')
window.open(jumpUrl,jumpTarget);
else location.href=jumpUrl;
}
}
function displayStatusMsg()
{
status=imgLink[adNum];
document.returnValue = true;
}
function checkButton()
{
for(i=1;i<=imgUrl.length-1;i++)
{if(i==adNum)
//document.getElementById('bt'+i).style.backgroundColor='#00FF00';
eval("bt"+i+".style.backgroundColor='#9B0000';bt"+i+".style.color='#FFFFFF';");
else
eval("bt"+i+".style.backgroundColor='#CCCCCC';bt"+i+".style.color='#000000';");
}
}
function dakularButtons()
{
if(buttonShow==1)
{
switch(buttonPos)
{
case 1:{buttonX=160+20-imgUrl.length*20;buttonY=16;break;}
case 2:{buttonX=160+20-imgUrl.length*20;buttonY=125;break;}
case 3:{buttonX=imgUrl.length*20-100;buttonY=16;break;}
case 4:{buttonX=imgUrl.length*20-100;buttonY=125;break;}
default:{buttonX=160+20-imgUrl.length*20;buttonY=16;break;}
}
for(i=1;i<=imgUrl.length-1;i++)
{
dakularCode=dakularCode+"<div id='bt"+i+"' style='display:inline; height=14px; width:20px; border:1px solid #000000; background-color:#f7f7f7; color:#000000; font-size:12px; line-height:14px; filter:Alpha(Opacity=50, FinishOpacity=50,Style=2);' align='center' onMouseOver=this.style.backgroundColor='#FFFFFF';this.style.color='#000000';this.style.cursor='hand' onMouseOut=checkButton(); onClick='clearTimeout(theTimer);nextAd("+i+");'>"+i+"</div>";
}
dakularCode="<div style='display:inline; position:relative;left:"+buttonX+"px; top:"+buttonY+"px; z-index:1;'>"+dakularCode+"</div>";
document.write(dakularCode);
}
}
</script>
<table border="0" cellpadding="0" cellspacing="0" id=newsTable style="position:relative; top:-10px;right:0px; font-size:12px;">
<script language="javascript">dakularButtons();</script><tr><td><a onMouseOver="displayStatusMsg();return document.returnValue;" onMouseOut="status='';" href="javascript:jump2url()"><img style="FILTER: revealTrans(duration=1,transition=18); border:1px solid #000000" src="javascript:nextAd()" width=160 height=150 border=0 name=imgUrlrotator alt="今日头条"></a></td></tr>
<tr><td bgcolor="#f0f0f0" align="center" style="border-bottom:1px solid #000000; border-left:1px solid #000000;border-right:1px solid #000000;" height="20"><label id="linktext"></label></td></tr>
</table>
<!--begin:图片轮换结束-->

解决方案 »

  1.   

    从INI文件里修改值
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;/**
    ?* 这是个配置文件操作类,用来读取和设置ini配置文件
    ?* @author 由月
    ?* @version 2004-08-18
    ?*/
    public final class Test17 {
    /**
    * 从ini配置文件中读取变量的值
     * @param file 配置文件的路径
     * @param section 要获取的变量所在段名称
     * @param variable 要获取的变量名称
     * @param defaultValue 变量名称不存在时的默认值
     * @return 变量的值
     * @throws IOException 抛出文件操作可能出现的io异常
     */
    public static String getProfileString(
    String file,
    String section,
    String variable,
    String defaultValue)
    throws IOException {
    String strLine, value = "";
    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
    boolean isInSection = false;
    try {
    while ((strLine = bufferedReader.readLine()) != null) {
    strLine = strLine.trim();
    strLine = strLine.split("[;]")[0];
    Pattern p;
    Matcher m;
    p = Pattern.compile("\\[\\s*.*\\s*\\]");
    m = p.matcher((strLine));
    if (m.matches()) {
    p = Pattern.compile("\\[\\s*" + section + "\\s*\\]");
    m = p.matcher(strLine);
    if (m.matches()) {
    isInSection = true;
    } else {
    isInSection = false;
    }
    }
    if (isInSection == true) {
    strLine = strLine.trim();
    String[] strArray = strLine.split("=");
    if (strArray.length == 1) {
    value = strArray[0].trim();
    if (value.equalsIgnoreCase(variable)) {
    value = "";
    return value;
    }
    } else if (strArray.length == 2) {
    value = strArray[0].trim();
    if (value.equalsIgnoreCase(variable)) {
    value = strArray[1].trim();
    return value;
    }
    } else if (strArray.length > 2) {
    value = strArray[0].trim();
    if (value.equalsIgnoreCase(variable)) {
    value = strLine.substring(strLine.indexOf("=") + 1).trim();
    return value;
    }
    }
    }
    }
    } finally {
    bufferedReader.close();
    }
    return defaultValue;
    }
    /**
    * 修改ini配置文件中变量的值
    * @param file 配置文件的路径
    * @param section 要修改的变量所在段名称
    * @param variable 要修改的变量名称
    * @param value 变量的新值
    * @throws IOException 抛出文件操作可能出现的io异常
    */
    public static boolean setProfileString(
    String file,
    String section,
    String variable,
    String value)
    throws IOException {
    String fileContent, allLine,strLine, newLine, reStr;
    String getValue;
    BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
    boolean isInSection = false;
    fileContent = "";
    try {while ((allLine = bufferedReader.readLine()) != null) {
    allLine = allLine.trim();
    if (allLine.split("[;]").length > 1)
    reStr = ";" + allLine.split(";")[1];
    else
    reStr = "";
    strLine = allLine.split(";")[0];
    Pattern p;
    Matcher m;
    p = Pattern.compile("\\[\\s*.*\\s*\\]");
    m = p.matcher((strLine));
    if (m.matches()) {
    p = Pattern.compile("\\[\\s*" + section + "\\s*\\]");
    m = p.matcher(strLine);
    if (m.matches()) {
    isInSection = true;
    } else {
    isInSection = false;
    }
    }
    if (isInSection == true) {
    strLine = strLine.trim();
    String[] strArray = strLine.split("=");
    getValue = strArray[0].trim();
    if (getValue.equalsIgnoreCase(variable)) {
    newLine = getValue + " = " + value + " " + reStr;
    fileContent += newLine + "\r\n";
    while ((allLine = bufferedReader.readLine()) != null) {
    fileContent += allLine + "\r\n";
    }
    bufferedReader.close();
    BufferedWriter bufferedWriter =
    new BufferedWriter(new FileWriter(file, false));
    bufferedWriter.write(fileContent);
    bufferedWriter.flush();
    bufferedWriter.close();return true;
    }
    }
    fileContent += allLine + "\r\n";
    }
    }
    catch(IOException ex){
    throw ex;
    } finally {
    bufferedReader.close();
    }
    return false;
    }
    /**
     * 程序测试
     */
    public static void main(String[] args) {
    //String value = Config.getProfileString("sysconfig.ini", "Option", "OracleDB", "default");
    //System.out.println(value);
    try {
    System.out.println(Test17.setProfileString("d:/1.ini", "1", "value", "111"));
    } catch (IOException e) {
    System.out.println(e.toString());
    }
    }
    }
      

  2.   

    //从字符串中查找某个字符 
    public class Test3 {
    public static void main(String[] args) {
    String a ="abd";
    if (a.indexOf("c") != -1){
    System.out.print(a);
    }
    else 
    System.out.print("没有呀");
    }
    }
      

  3.   

    import java.io.*;
    public class Test4 {//已经定义为String A;如果是A=68这样就显示68,如果是68.11111这样的只保留小数点后两位
    public static void main(String[] arg) throws IOException {
    BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
    String A;
    A=r.readLine();
    if(A.indexOf('.')!=-1) 
    System.out.println(A.substring(0,A.indexOf('.')+3));
    else 
    System.out.println(A);}
    }