fileupload图片上传并把地址保存到数据库

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hoho_12/article/details/52908758

GetFile.java(处理上传请求的servlet)

package com.servlet;

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.util.ConnDB;

public class GetFile extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String str=(String) request.getSession().getAttribute("id");		
		System.out.println("拿到的id值为"+str);
		int id=Integer.parseInt(str);		
		
		String path=this.getServletContext().getRealPath("/")+"userFile";
		System.out.println("WebRoot的根路径:"+path);
		
		File file=new File(path);
		if(!file.exists()){
			file.mkdir();
		}
		
		DiskFileItemFactory factory=new DiskFileItemFactory();
		
		ServletFileUpload upload=new ServletFileUpload(factory);
		
		upload.setHeaderEncoding("utf-8");
		
		//判断提交的数据是否是表单数据
		if(!ServletFileUpload.isMultipartContent(request)){
			System.out.println("提交的不是表单数据");
			return;
		}
		try {
			List<FileItem> items=upload.parseRequest(request);
			
			for(FileItem item :items){
				
				if(item.isFormField()){
					String name=item.getString();
					String value=new String(name.getBytes("ISO-8859-1"),"utf-8");
					System.out.println("文本框中的值:"+value);
					
					
				}else{					
					System.out.println("提交的是文件");
					//item.getFieldName();
					String filename=item.getName();
					System.out.println("获取到的上传文件名:"+filename);					
					
					String prefix=filename.substring(filename.lastIndexOf(".")+1);
					
					String fName=id+"."+prefix;					
					
					InputStream is=item.getInputStream();
					FileOutputStream fos=new FileOutputStream(path+"/"+fName);
					int index=0;
					
					while((index=is.read())!=-1){
						fos.write(index);
					}
					is.close();
					fos.close();
					item.delete();		
					
					Connection conn=ConnDB.getConn();
					String sql="update user set photo=? where id=?";
					PreparedStatement psta;			
					psta = conn.prepareStatement(sql);
					
					psta.setString(1, "userFile/"+fName);
					psta.setInt(2, id);
					psta.executeUpdate();				
				}
			}
			
			
		} catch (FileUploadException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		response.sendRedirect("showOne.jsp?id="+id);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}

 用户自定义图像
  <form action="GetFile" method="post" enctype="multipart/form-data" >
  			<input type="file" name="myfile"><br/> 
  			<input type="text" name="uname"> <br/> 			
 			<input type="submit" value="上传">
  </form> 


展开阅读全文

没有更多推荐了,返回首页