-2

help to make such a thing (in the previous answer, I was told that I was poorly explained, and now try to explain poponyatneee) I have a form, just a form, the code it is:

<form action="searchf" method="Post">
<input type="text" name="text" autofocus >
<input type="submit" value="">
</form>

The user enters into this form different file names, such as: index.txt, hello.html (files with duplicate names not, and basically it will txt files). Next, the servlet takes string that the user enters:

String myName = request.getParameter("text");

Continue program called input files looking for the files in the folder, but the fact is that it is necessary to enter index.html program to find the file in the folder, not just index, here's help to enter the extension was not necessarily trying to do so :

String myNamee = request.getParameter("text");
String myName = myNamee.replaceFirst("[.][^.]+$", "");

It did not work (I will be glad to any replies

Eric Scot

Eric Scot
  • 197
  • 8

2 Answers2

0

Why not cut out everything from the last dot in the name to the end:

final String baseName = fileName.substring(0, fileName.lastIndexOf("."));
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0

If you need to separate the file extension from the filename, you can do something like this:

String file = request.getParameter("text");
String fileNoExt, fileExt = "";
if(file.indexOf(".") == -1) {
   fileNoExt = file;
else {
   fileNoExt = file.substring(0,file.lastIndexOf("."));
   fileExt  = file.substring(file.lastIndexOf(".")+1);
}

I recommend that you test this code outside the context of Servlets. Just create a simple Java program with a main method to search a directory for a file and you can forget the user-input and form info. Just hard code some test data to make sure you know how to look through a directory and your code to handle the filenames is working correctly.

You may find this other stack overflow question helpful for understanding how to serach through a directory for a file.

Community
  • 1
  • 1
Thorn
  • 4,015
  • 4
  • 23
  • 42
  • 1
    @EricScot - explain yourself. In what sense did it not work? What were the symptoms? What were the errors? – Stephen C Nov 14 '12 at 14:16
  • I tried to enter and hello.txt and hello, in both cases, I was given a white screen, so there is an error ( – Eric Scot Nov 14 '12 at 14:19
  • So what did the logfile say? – Stephen C Nov 14 '12 at 14:20
  • @EricScot - And did you test it outside of a servlet like Thorn suggested? – Stephen C Nov 14 '12 at 14:21
  • nothing 14.11.2012 17:07:36 org.apache.catalina.core.ApplicationContext log INFO: ContextListener: contextInitialized() 14.11.2012 17:07:36 org.apache.catalina.core.ApplicationContext log INFO: SessionListener: contextInitialized() 14.11.2012 17:13:52 org.apache.catalina.core.ApplicationContext log – Eric Scot Nov 14 '12 at 14:22
  • See, I realized what the problem is I have done it so it is necessary to output files from a directory without an extension and only then compare them with what the user entered) – Eric Scot Nov 14 '12 at 14:25
  • If you tested the file access code outside of a Servlet and it worked, then there is a problem with how you are looking for files in the Servlet. Post your Servlet code or just your doGet method. – Thorn Nov 14 '12 at 15:11