0
我做了應該將文件上載到一個目錄JSP
,它表明我嘗試連接時出現錯誤: 在這裏,你可以看到我的代碼:JSP上傳文件錯誤
如果有人能幫助我,我會appriciate它。 package src;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.RequestDispatcher;
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.FileItemFactory;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
public class Controller extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
String saveFile="c:\\uploaddir\\";
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String actiune="afiseaza";
PrintWriter out=response.getWriter();
if (request.getParameter("actiune")!=null)
{
actiune = request.getParameter("actiune");
}
if (actiune.equals("afiseaza"))
{
doAfiseaza(request, response);
}
if (actiune.equals("sterge"))
{
doDeleteFile(request, response);
}
try{
boolean ismultipart=ServletFileUpload.isMultipartContent(request);
if(!ismultipart)
{
}
else
{
FileItemFactory factory= new DiskFileItemFactory();
ServletFileUpload upload= new ServletFileUpload(factory);
List items=null;
try{
items=upload.parseRequest(request);
}catch(Exception e)
{
}
Iterator itr=items.iterator();
while(itr.hasNext())
{
FileItem item= (FileItem)itr.next();
if(item.isFormField())
{
}
else
{
String itemname=item.getName();
if((itemname==null)||itemname.equals(""))
{
continue;
}
String filename=FilenameUtils.getName(itemname);
File f=checkExist(filename);
item.write(f);
}
}
}
}catch(Exception e)
{
}
finally{
out.close();
}
}
private void doDeleteFile(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String fName=URLDecoder.decode(request.getParameter("fis"));
File f = new File("c:\\uploaddir\\"+fName);
f.delete();
doAfiseaza(request, response);
}
private void doAfiseaza(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String dir = "c:\\uploaddir";
int er = 0;
File[] lista = null;
try
{
File f = new File(dir);
lista = f.listFiles();
}
catch(Exception ex)
{
er=-1;
}
request.setAttribute("eroare", new Integer(er));
request.setAttribute("lista", lista);
RequestDispatcher rd = request.getRequestDispatcher("WEB-INF/lista.jsp");
rd.forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Prima aplicatie";
}// </editor-fold>
private File checkExist(String fileName) {
File f=new File(saveFile+"/"+fileName);
if(f.exists())
{
StringBuffer sb=new StringBuffer(fileName);
sb.insert(sb.lastIndexOf("."), "-"+new Date().getTime());
f=new File(saveFile+"/"+sb.toString());
}
return f;
}
}
所以現在我沒有錯誤,但它不上傳我的文件夾,所以我不知道該怎麼辦。
請將您的代碼粘貼到問題中,以便閱讀更容易。我剛纔看了一下代碼和錯誤消息:「解析時到達文件末尾」,我猜你已經在代碼中的某個地方遺漏了右括號「}」。 – Adam
我把它丟在那裏,但它顯示了另一個錯誤,當我嘗試提交該文件。我編輯了這個問題,請看上面。 –
如果你能幫助我,請@亞當,這將是很好的。 –