1、jsp前端
< % --
Created by IntelliJ IDEA.
User: Lenovo
Date: 2020 / 6 / 19
Time: 22 : 53
Learn from https: / / www. bilibili. com/ video/ BV18z411i7gh? t= 23 & p= 192
To change this template use File | Settings | File Templates.
-- % >
< % @ page contentType= "text/html;charset=UTF-8" language= "java" % >
< html>
< head>
< title> 文件上传< / title>
< / head>
< body>
< ! -- 文件上传对表单的要求-- >
< ! --
1 、表单中的请求提交方式必须是POST
2 、表单中应指定所提交的请求位multipart请求,通过在< form/ > 标签中添加enctype属性
其值为multipart/ form- data
3 、 表单
-- >
< form method= "POST" action= "http://localhost:8888/hello/UploadImageServlet" enctype= "multipart/form-data" >
编号< input type= "text" name= "BNO" > < / br>
名字< input type= "text" name= "BNAME" > < / br>
照片< input type= "file" name= "picutreUrl" > < / br>
< input type= "submit" value= "注册" >
< / form>
< / body>
< / html>
2、servlet后台
package Servlet. bookServlet;
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 javax. servlet. ServletException;
import javax. servlet. annotation. WebServlet;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import java. io. *;
import java. text. SimpleDateFormat;
import java. util. Calendar;
import java. util. Date;
import java. util. List;
@WebServlet ( name = "UploadImageServlet" )
public class UploadImageServlet extends HttpServlet {
@Override
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this . doPost ( request, response) ;
}
@Override
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if ( ! ServletFileUpload. isMultipartContent ( request) ) {
throw new RuntimeException ( "当前请求不支持文件上传" ) ;
}
System. out. println ( "开始上传文件" ) ;
try {
DiskFileItemFactory factory = new DiskFileItemFactory ( ) ;
String tempPath = "D:\\tomcat\\apache-tomcat-9.0.35-windows-x64\\apache-tomcat-9.0.35\\webapps\\librarySystem\\web\\net\\temp" ;
File tempFile = new File ( tempPath) ;
factory. setRepository ( tempFile) ;
factory. setSizeThreshold ( 1024 * 1024 * 1 ) ;
ServletFileUpload upload = new ServletFileUpload ( factory) ;
upload. setHeaderEncoding ( "utf-8" ) ;
upload. setFileSizeMax ( 1024 * 1024 * 2 ) ;
String bNo= "defaultBNo" , bName= "defaultBName" ;
List< FileItem> items = upload. parseRequest ( request) ;
for ( FileItem item: items) {
if ( item. isFormField ( ) ) {
String fileName = item. getFieldName ( ) ;
String fileValue = item. getString ( "utf-8" ) ;
System. out. println ( fileName + " -- " + fileValue) ;
if ( fileName. equalsIgnoreCase ( "BNO" ) ) {
bNo = fileValue;
}
if ( fileName. equalsIgnoreCase ( "BNAME" ) ) {
bName = fileValue;
}
}
else {
String fileName = item. getName ( ) ;
System. out. println ( "上传文件名字:" + fileName) ;
String suffix = fileName. substring ( fileName. lastIndexOf ( '.' ) ) ;
String newFileName = bNo+ "_" + bName+ suffix;
System. out. println ( newFileName) ;
InputStream is = item. getInputStream ( ) ;
String path = "D:\\tomcat\\apache-tomcat-9.0.35-windows-x64\\apache-tomcat-9.0.35\\webapps\\librarySystem\\web\\net\\bookImage" ;
Calendar now = Calendar. getInstance ( ) ;
int year = now. get ( Calendar. YEAR) ;
int month = now. get ( Calendar. MONTH) + 1 ;
int day = now. get ( Calendar. DAY_OF_MONTH) ;
path = path+ "/" + year+ "/" + month+ "/" + day;
File dirFile = new File ( path) ;
if ( ! dirFile. exists ( ) ) {
dirFile. mkdirs ( ) ;
}
File desFile = new File ( path, newFileName) ;
OutputStream os = new FileOutputStream ( desFile) ;
int len= - 1 ;
byte [ ] buf = new byte [ 1024 ] ;
while ( ( len= is. read ( buf) ) != - 1 ) {
os. write ( buf, 0 , len) ;
}
os. close ( ) ;
is. close ( ) ;
item. delete ( ) ;
}
}
} catch ( FileUploadException e) {
e. printStackTrace ( ) ;
}
}
}