替换单字符\到路径中的双字符\\ [英] replace single char \ to double char \\ in path

查看:141
本文介绍了替换单字符\到路径中的双字符\\的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览一个jsp页面上的文件,并将此文件地址发送到第二个jsp页面,在第二个jsp页面上,我正在以字符串形式接收此文件路径,代码如下.

I am browsing a file on a jsp page and sending this file address to second jsp page, on second jsp page i am receiving this file path as a string the code is given below.

<%
String st=request.getParameter("mptest");
String rst=  request.getRealPath(st); 

\\here in rst i am geting a path as c:\vishal\ragh.xls and i wish to replace \ with \\ . Hence i used replace method for it the way given below

rst = rst.replace("\","\\"); 

\\but netbeans id showing an error that "illigal scape character" . Is there any other way so that
i could do my job.Please anyone help me.
    
%>

推荐答案

尝试
rst = rst.replace("\\","\\\\");

[ ^ ]可能会为您提供帮助.

[edit]替换字符串中有四个\.[/edit]
Try
rst = rst.replace("\\","\\\\");

This[^] may help you.

[edit]four \es in the replacement string.[/edit]


它不能解决我的问题……当我使用它时,它将删除错误,但未提供所需的输出,我的copmlete代码在下面给出






it doesn''t solve my problem...... when i used it , it remove the error but didn''t give desired output my copmlete code is given below






<%@page import="org.apache.poi.ss.usermodel.Cell"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.io.*"%>
<%@page import="java.util.regex.Matcher"%>
<%@page import="java.util.*"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFWorkbook"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFRow"%>
<%@page import="org.apache.poi.hssf.usermodel.HSSFCell"%>
<form  action="tab.jsp" method="get" enctype="multipart/form-data>
            <div align="left">
                <input type="file" name="F1" > <br>
                <input type="submit" name="send">

<table border="1">
<% String rst=null;
    try
    {
  String st=request.getParameter("mptest");
  String o= Matcher.quoteReplacement("'\'");
   rst=  request.getRealPath(st);
  rst = rst.replace("\\","\\\\");
    out.println(rst);}
    catch(Exception e)
   {e.printStackTrace();}

    int i=0;
    String  value1="";
    double value2=0;
    if(rst != null && !rst.equals(""))
    {
    try
    {
    FileInputStream fs =new FileInputStream(rst);
    HSSFWorkbook wb = new HSSFWorkbook(fs);
    for(int k = 0; k < wb.getNumberOfSheets(); k++)
    {
    int j=i+1;
    HSSFSheet sheet = wb.getSheetAt(k);
    int rows  = sheet.getPhysicalNumberOfRows();
    for(int r = 0; r < rows; r++)
    {
    HSSFRow row   = sheet.getRow(r);
    %>
    <tr>
    <%

    int cells = row.getPhysicalNumberOfCells();
    for(int ce = 0; ce < cells; ce++)
    {

    HSSFCell cell1  = row.getCell(ce);
    switch(cell1.getCellType())
    {
        case Cell.CELL_TYPE_STRING:
     value1 = cell1.getStringCellValue();
   %>
   <td><%=value1%></td>
   <%
   break;
    case Cell.CELL_TYPE_NUMERIC:
     value2 = cell1.getNumericCellValue();
   %>
   <td><%=value2%></td>
   <%
  break;
    }

    %>

    <%
    }
    %>
    </tr>
    <%
        i++;
    }
        }
    }
    catch(Exception e){
    System.out.println(e);
    }
    }
    %>
    </table>
</form>


这篇关于替换单字符\到路径中的双字符\\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆