Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java struts如何设置上传文件的特定名称_Java_File Upload_Struts2_Filenames - Fatal编程技术网

Java struts如何设置上传文件的特定名称

Java struts如何设置上传文件的特定名称,java,file-upload,struts2,filenames,Java,File Upload,Struts2,Filenames,我想为目标文件夹中上载的文件指定特定名称。。这是我的操作文件代码。。这里我想给大家起个名字,比如CPIC_1.jpg,CPIC_2.jpg,CPIC_3.jpg,CPIC_4.jpg等等,但每次它都指定了这个名字:CPIC_1.jpg。。所以我如何声明变量ext,使其始终是不同的 CommercialFileBean b = (CommercialFileBean) form; FormFile f = b.getF(); String s = request.getParameter("act

我想为目标文件夹中上载的文件指定特定名称。。这是我的操作文件代码。。这里我想给大家起个名字,比如CPIC_1.jpg,CPIC_2.jpg,CPIC_3.jpg,CPIC_4.jpg等等,但每次它都指定了这个名字:CPIC_1.jpg。。所以我如何声明变量ext,使其始终是不同的

CommercialFileBean b = (CommercialFileBean) form;
FormFile f = b.getF();
String s = request.getParameter("action");
HttpSession session = request.getSession(false);
String n = (String) session.getAttribute("str");
String email = session.getAttribute("uname").toString();
String status = (String) session.getAttribute("status");
String type = request.getParameter("type");
String pid;
long ext=0;
int id;
if (s.equalsIgnoreCase("finish")) {
    return mapping.findForward(next);
} else {   /// first else
    String a = getServlet().getServletContext().getRealPath("/");
    File file = new File(a + "uploaded/CPIC_"+ ++ext+".jpg");
    if (!file.exists()) {
        FileOutputStream out = new FileOutputStream(file);
        out.write(f.getFileData());
        out.close();
    }
    try {

        if (n.equalsIgnoreCase("rent")) {
            Session sess = UtilClass.createSession();
            Transaction tx = sess.beginTransaction();
            if (status.equalsIgnoreCase("new")) {
                String sql1 = "select MAX(id) from Rentcommercialrecord where loginid=:email";
                Query q1 = sess.createQuery(sql1);
                q1.setParameter("email", email);
                //  JOptionPane.showMessageDialog(null, "max id is :");
                List<Rentcommercialrecord> l = q1.list();
                Rentcommercialrecord rc = l.get(l.size()-1);
                id = rc.getId();
            } else {
                pid = (String) session.getAttribute("id");
                id = Integer.parseInt(pid);
            }
            JOptionPane.showMessageDialog(null, " latest id is :" + id);
            if (type.equalsIgnoreCase("frontpic")) {
                try {
                    String file1 = f.getFileName();
                    JOptionPane.showMessageDialog(null, "file name is : "+file1);
                    Rentcommercialrecord rc1 = (Rentcommercialrecord) sess.get(Rentcommercialrecord.class, id);
                    rc1.setImg1("CPIC_" +ext+".jpg");
                    sess.update(rc1);
                   // JOptionPane.showMessageDialog(null, "img1");

                } // img1 try ends
                catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Second error is : " + e.getMessage());
                }
            } // fontpic if ends
            else {
                try {

                    String file1 = f.getFileName();
                    JOptionPane.showMessageDialog(null, "file name is : "+file1);
                    Rentcommercialrecord rc1 = (Rentcommercialrecord) sess.get(Rentcommercialrecord.class, id);
                    rc1.setImg2("CPIC_" +ext+".jpg");
                    sess.update(rc1);
                   // JOptionPane.showMessageDialog(null, "img2");

                } // img2 try ends
                catch (Exception e) {
                    JOptionPane.showMessageDialog(null, "Second error is : " + e.getMessage());
                }
            }   // else img2 ends

            // l.size if ends
            tx.commit();
        }
CommercialFileBean b=(CommercialFileBean)表单;
FormFile f=b.getF();
字符串s=request.getParameter(“操作”);
HttpSession session=request.getSession(false);
String n=(String)session.getAttribute(“str”);
字符串email=session.getAttribute(“uname”).toString();
字符串状态=(字符串)session.getAttribute(“状态”);
字符串类型=request.getParameter(“类型”);
串pid;
长ext=0;
int-id;
如果(s.equalsIgnoreCase(“完成”)){
返回映射。findForward(下一步);
}else{///第一个else
字符串a=getServlet().getServletContext().getRealPath(“/”);
文件文件=新文件(a+“已上载/CPIC”+++ext+”.jpg);
如果(!file.exists()){
FileOutputStream out=新的FileOutputStream(文件);
out.write(f.getFileData());
out.close();
}
试一试{
如果(n.同等信号(“租金”)){
会话sess=UtilClass.createSession();
事务tx=sess.beginTransaction();
if(状态相等信号情况(“新”)){
String sql1=“从Rentcommercialrecord中选择MAX(id),其中loginid=:email”;
queryQ1=sess.createQuery(sql1);
q1.设置参数(“电子邮件”,电子邮件);
//showMessageDialog(null,“最大id为:”);
List l=q1.List();
Rentcommercialrecord rc=l.get(l.size()-1);
id=rc.getId();
}否则{
pid=(字符串)session.getAttribute(“id”);
id=Integer.parseInt(pid);
}
showMessageDialog(null,“最新id为:”+id);
if(类型:equalsIgnoreCase(“frontpic”)){
试一试{
字符串file1=f.getFileName();
showMessageDialog(null,“文件名为:”+file1);
Rentcommercialrecord rc1=(Rentcommercialrecord)sess.get(Rentcommercialrecord.class,id);
rc1.setImg1(“CPIC_u“+ext+”.jpg”);
sess.更新(rc1);
//showMessageDialog(null,“img1”);
}//img1 try结束
捕获(例外e){
showMessageDialog(null),第二个错误是:“+e.getMessage()”;
}
}//fontpic如果结束
否则{
试一试{
字符串file1=f.getFileName();
showMessageDialog(null,“文件名为:”+file1);
Rentcommercialrecord rc1=(Rentcommercialrecord)sess.get(Rentcommercialrecord.class,id);
rc1.setImg2(“CPIC_u“+ext+”.jpg”);
sess.更新(rc1);
//showMessageDialog(null,“img2”);
}//img2 try结束
捕获(例外e){
showMessageDialog(null),第二个错误是:“+e.getMessage()”;
}
}//否则img2结束
//l.如果结束,尺寸为
tx.commit();
}

将变量
ext
设置为
静态

static long ext=0;

这将使变量对所有实例通用


注意:您需要在重新启动期间将此值存储在db/文件中的某个位置,并在应用程序启动期间获取该值,以使其与重新启动应用程序时保持一致

您可以将
ext
变量
设置为静态

注意:静态变量的作用域是针对当前类加载器的。也就是说,如果使用的是diff类加载器,则会发生变化


另一个选项是将
ext
值存储在
session
中,每次上载新图像文件时,从session中获取该值并使用它。您还需要将新值放回session中。这种方法适用于每个用户。例如,如果您的应用程序具有不同的用户,则对于不同的用户,它将基于不同的值在会话

中,您可以使用静态变量,但在应用程序重新启动时,该变量将不一致

我会改变方法,读取文件名,然后从文件名中提取数字,得到最高值,递增,然后编写一个新文件

用于避免重新发明轮子

启动示例:

String path = getServlet().getServletContext().getRealPath("/") + "uploaded/";
String partialName = "CPIC_";    
int markerLength = partialName.length();
int maxValue = 0;

// Find all files, if any, with name starting with "CPIC_" in the desired folder
List<File> files = FileUtils.listFiles(new File(path), 
                                       new PrefixFileFilter(partialName), 
                                       null);

if (!files.isEmpty()){
    for (File file : files) {
        // Strip marker and extension
        int num = Integer.parseInt(
                     file.getName().substring(markerLength,
                                              file.getName().indexOf("."))
                  );
        // compare the number, if greater, set as new max value
        if (num > maxValue) {
            maxValue = num;
        }
    }
}

String newFile = partialName + ++maxValue + ".jpg";
System.out.println("Next file name would be : " + newFile);
String path=getServlet().getServletContext().getRealPath(“/”)+“Upload/”;
String partialName=“CPIC_”;
int-markerLength=partialName.length();
int maxValue=0;
//在所需文件夹中查找名称以“CPIC_”开头的所有文件(如果有)
列表文件=FileUtils.listFiles(新文件(路径),
新的PrefixFileFilter(partialName),
无效);
如果(!files.isEmpty()){
用于(文件:文件){
//带标记和扩展
int num=Integer.parseInt(
file.getName().substring(markerLength,
file.getName().indexOf(“.”)
);
//比较数字,如果更大,则设置为新的最大值
如果(num>maxValue){
maxValue=num;
}
}
}
字符串newFile=partialName+++maxValue+“.jpg”;
System.out.println(“下一个文件名为:“+newFile”);

我在ext的操作文件中使用了该static关键字,但它给了我类似illgegal表达式开头的错误。我认为您是在方法级别声明它。
静态
变量在方法级别不允许。
静态
变量应该在类级别声明,所以我的问题是我必须在哪里声明它我们是这个变量