简体   繁体   English

修改了文件名的JSP文件上传

[英]JSP file upload with modified filename

I am developing a Job Portal website using JSP and Oracle XE.Now,a Job Seeker is supposed to upload his Resume/CV.Say,this filename is Resume.pdf.Next, another job seeker uploads his Resume.pdf(same name).I want to avoid saving files with same name.One adviced me to concatinate the date with the filename when storing in database.But I want to concatenate the Jobseeker ID (created by Sequence in oracle) with this filename. 我正在使用JSP和Oracle XE开发一个Job Portal网站。现在,一个求职者应该上传他的Resume / CV。说,这个文件名是Resume.pdf。接下来,另一个求职者上传他的Resume.pdf(同名)我想避免保存同名文件。有人建议我在存储在数据库中时用文件名来代替日期,但是我想用这个文件名连接Jobseeker ID(由Sequence在oracle中创建)。 The issue is:When file is uploaded, I don't need the Connection string.I need it only during writing the filename to the database.Without this condition, I have successfully uploaded files. 问题是:上传文件时不需要连接字符串,仅在将文件名写入数据库时​​才需要它,没有这种情况,我就成功上传了文件。 I am giving the codes for the sequence and insert-procedure. 我提供了序列和插入过程的代码。

create sequence js_id_seq
increment by 1 
start with 100
nocache
nocycle;
/

create or replace procedure js_file_prefx(prefx out varchar2)
is
begin
select 'JS'||js_id_seq.**nextval** into prefx from dual;
end;
/
create or replace procedure ins_job_seekr (
name varchar2,
paswd varchar2,
profile varchar2,
email varchar2,
address varchar2,
phone number,
resume varchar2
)
is
begin
insert into employer values(
'JS'||js_id_seq.**currval**,
name,
paswd,
profile,
email,
address,
phone,
resume
 );
end;
/

JSP code part: JSP代码部分:

 String emp_file="";

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String  Filename1 ="";
File file;
String filePath="";

if (!isMultipart) 
{

} 
else {


DiskFileItemFactory factory = new DiskFileItemFactory();

------  //not giving code this part as this was working fine
 else {

        try {   //if i put connection string here, it says that its a different    
              //instance of hr

             CallableStatement cs=con.prepareCall("{call emp_file_prefx(?)}");

       cs.registerOutParameter(1,Types.VARCHAR);
       cs.execute();
       emp_file= cs.getString(1); 

            Filename1 = item.getName();
            filePath=config.getServletContext().getRealPath("/")+"upload_data\\";

            Filename=Filename1.substring( Filename1.lastIndexOf("\\")+1);

           Filename=Filename + emp_file;

            if(Filename!="")
                {  file = new File( filePath +Filename) ;
                   item.write(file);
                }

              }
        catch(Exception err)
            {
                out.println(err.getMessage());
            }

        }
   }

  try
    {
     DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

     Connection con=DriverManager.getConnection
     ("jdbc:oracle:thin:@127.0.0.1:1521:XE","hr","hr");
    CallableStatement cs1=con.prepareCall("{call ins_job_seekr(?,?,?,?,?,?)}");

    cs1.setString(1,c_name);
cs1.setString(2,paswrd);
cs1.setString(3,c_prof);
cs1.setString(4,mail);
cs1.setString(7,addres);
    cs1.setInt(8,Integer.parseInt(phone_no));
cs1.setString(9,Filename);


cs1.execute();
    out.println("Insert successful");



    //con.close();
   }
  catch(Exception err)
  {
   out.println(err.getMessage());
  }

  }

I guess that the job seeker has to log in to your site. 我想求职者必须登录到您的网站。 You most likely may have a session object around which will help you to identify your user. 您极有可能在其周围有一个会话对象,该对象将帮助您识别用户。 You could store the Jobseeker ID in the session when she log in and read it from there when you create the filename on upload. 当她登录,当你创建上传的文件名进行阅读,你可以存储求职者ID会话。

When the file was successfully written to the server you just have to put the filename in the database. 将文件成功写入服务器后,您只需将文件名放入数据库中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM