jsp上传文件

2019-04-15 16:31发布

前台 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>




Insert title here






<%
String str=request.getParameter("pic");
if(str!=null)
{
str="";
}
else
{
str="";
}

%>
<%= str %>






后台 
package UploadFile;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
 * Servlet implementation class UploadFile
 */
@WebServlet("/UploadFile")
public class UploadFile extends HttpServlet {
private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UploadFile() {
        super();
        // TODO Auto-generated constructor stub
    }


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}


/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
String savePath=this.getServletContext().getRealPath("/img");
response.getWriter().println(savePath);

File file=new File(savePath);

if(!file.exists() && !file.isDirectory())
{
file.mkdir();
}
String message="";
try
{
DiskFileItemFactory factory=new DiskFileItemFactory();
ServletFileUpload upload=new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
if(!ServletFileUpload.isMultipartContent(request))
{
response.getWriter().println("up load file img error!");
return ;
}
response.getWriter().println("
************

");
List list=upload.parseRequest(request);
for(FileItem item :list)
{
response.getWriter().println("
---------------------

");
if(item.isFormField())
{
String name=item.getFieldName();
String value=item.getString("utf-8");
response.getWriter().println(name + "#####" + value+"

");

}
else
{
String filename=item.getName();
response.getWriter().println(filename);
if(filename==null || filename.trim().equals(""))
continue;

filename=filename.substring(filename.lastIndexOf("\")+1);
response.getWriter().println("

checkfilename="+filename);

InputStream in=item.getInputStream();
FileOutputStream out=new FileOutputStream(savePath+"\"+filename);
response.getWriter().println("
savepath:"+savePath+"\"+filename);
byte buffer[]=new byte[1024];

int len=0;
while((len=in.read(buffer))>0)
{
out.write(buffer,0,len);
}
in.close();
out.close();
item.delete();
message="uploadfile success";
response.sendRedirect("../ryrs/uploadfile/uploadfile.jsp?pic="+filename);
}
}
}
catch(Exception ex)
{
message="uploadfile failed";
response.getWriter().println(ex.getMessage());
}

request.setAttribute("message", message);
// request.getRequestDispatcher("../uploadfile/message.jsp").forward(request, response);
}


}