我创建了一个JSP
,它应该将文件上传到目录中,并在尝试连接时显示错误:
在这里你可以看到我的代码:
如果有人可以帮助我,我会批评它。 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;
}
}
所以现在我没有错误,但它没有上传我的文件夹,所以我不知道该怎么做。
答案 0 :(得分:0)
由于您最初的问题是关于JSP,这里有一些演示代码。
<%@ page import="java.util.*,
java.io.*,
org.apache.commons.fileupload.*,
org.apache.commons.fileupload.servlet.*,
org.apache.commons.fileupload.disk.*,
org.apache.commons.fileupload.util.*"%>
<%
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
out.print("Request content length is " + request.getContentLength() + "<br/>");
if(isMultipart){
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(request);
FileItemStream item = null;
String name = "";
InputStream stream = null;
while (iter.hasNext()){
item = iter.next();
name = item.getFieldName();
stream = item.openStream();
if(item.isFormField()){out.println("Form field " + name + " with value "
+ Streams.asString(stream) + "<br/>");}
else {
name = item.getName();
if(name != null && !"".equals(name)){
String fileName = new File(item.getName()).getName();
out.println("Client file " + item.getName() + " <br/>with file name "
+ fileName + " uploaded.<br/>");
File file = new File("C:/uploaddir/" + fileName);
FileOutputStream fos = new FileOutputStream(file);
long fileSize = Streams.copy(stream, fos, true);
out.print(fileName
+ " was uploaded to the fileUploads folder. Size was " +
fileSize + "<br/>");
}
}
}
} else out.print("Wrong request type!");
%>
用
调用JSP<html><body>
<form enctype="multipart/form-data" action="fileUpload3.jsp" method="post">
<input type='file' name='file1'/><br/>
<input type='file' name='file2'/><br/>
<input type='file' name='file3'/><br/>
Type message:<input type="text" name="message" /><br/>
<input Type='submit' value='Submit'/>
</form>
</body></html>
将这两个文件放在您网络应用的根文件夹中。