0

我已经使用 apache commons 做了一个文件上传网页,这里发生的情况就像有时它上传文件,有时它不上传文件,我不知道问题出在哪里,是否在代码中(但它上传文件)或在网络中,因为如果文件在服务器上上传,我的输出页面将显示“文件上传成功”,我已经多次测试了我的代码,并且对于单次和多次上传都可以正常工作。问题从页面开始从网络服务器(apache tomcat)托管。

import com.mysql.jdbc.PreparedStatement;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class uploadpagedb extends HttpServlet {  
 private String fileName="";        
 private String main="";  
 private String location;  
 private String sub;  
 private File uploadFile;  
 private String filen;
 private String filetype;
@Override
public void init(){  
}  
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{        
    ResultSet rs=null;
    PreparedStatement psmnt=null;
 try 
  {  
    PrintWriter out = response.getWriter();          
    DiskFileItemFactory factory = new DiskFileItemFactory();          
    ServletFileUpload fileUpload = new ServletFileUpload(factory);         
    List items = fileUpload.parseRequest(request);  
    Iterator ir = items.iterator();  
     while(ir.hasNext()){  
     FileItem item = (FileItem)ir.next();   
String name = item.getFieldName();  
if(item.isFormField())
{  
 if(name != null)  
 {  
 if(name.equals("fileName"))
 {  
  fileName = item.getString();  
 }
 else if(name.equals("main"))
 {  
  main = item.getString();  
 }
 else if(name.equals("sub"))
 {
     sub=item.getString();
 }
 else if(name.equals("filetype"))
 {
     filetype=item.getString();
 }
 }  
}
else
{  
    String file1=item.getFieldName();
    String file2=item.getName();
    filen=file2;
 location = File.separator+"home"+File.separator+"adapco"+File.separator+"Desktop"+ File.separator +"output"+ File.separator +main+File.separator+sub+File.separator+filen;
 uploadFile = new File(location);        
 long size = item.getSize();  
 if(size <= 1024*1024*1024)
 {  
  item.write(uploadFile);  
  //out.println("Your File is uploaded successfully ");  
 }else{  
  out.println("Your File is not uploaded.File size should be less than 1GB");  
 }
 String concat=main+"/"+sub;
     Class.forName("com.mysql.jdbc.Driver");
     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/ksa","root","root");
     java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime());
     String status="n";
     int count=0;

     Statement st=con.createStatement();
     rs=st.executeQuery("select count(filename) as filename from file1 where filename='"+filen+"' and category='"+concat+"'");

     while(rs.next())
     {
     //String category1=rs.getString(1);
     count=rs.getInt(1);
     }
     if(count==1)
     {
        int dup=st.executeUpdate("update file1 set filename='"+filen+"' where category='"+concat+"'");
        out.println("file updated successfully");
     }
     else
     {
     psmnt=(PreparedStatement) con.prepareStatement("insert into file1(filedesc,category,filepath,filename,Date,filetype,status)values(?,?,?,?,'"+sqlDate+"',?,'"+status+"')");
     psmnt.setString(1, fileName);
     psmnt.setString(2, concat);
     psmnt.setString(3, location);
     psmnt.setString(4, filen);
     psmnt.setString(5, filetype);
     int ff=psmnt.executeUpdate();
    // InetAddress ownIP=InetAddress.getLocalHost();
     String ownIP=request.getRemoteAddr();
     psmnt=(PreparedStatement) con.prepareStatement("insert into updatelog values('"+ownIP+"')");
     int sysip=psmnt.executeUpdate();
     out.println("Your File is uploaded successfully ");  
     }
   }  
  }         
    //out.println("uploaded successfully");        
  //response.sendRedirect("uploadfile.jsp");                          
 } 
 catch (Exception e) 
 {  
  System.out.println("Error"+e.getMessage());
 }  
 }  
 }  
4

1 回答 1

-1

你可以尝试使用

uploadFile = new File(getServletContext().getRealPath(location)); 

这将在您的网络应用程序的根文件夹中创建文件(带有您的文件夹结构)。
如果文件位置正常,则更改该行

String file2=item.getName();  

String file2 = new File(item.getName()).getName(); 

因为 IE 浏览器在客户端机器上发送完整路径。

于 2012-08-06T15:08:38.350 回答