jquery和java实现一个文件上传功能
时间: 2023-10-22 18:03:34 浏览: 98
jquery 文件上传
1. jQuery实现文件上传功能:
HTML代码:
```
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="button" value="上传" id="upload">
</form>
<div id="message"></div>
```
JavaScript代码:
```
$(document).ready(function(){
$('#upload').click(function(){
var file_data = $('#file').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url: 'upload.php',
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(response){
$('#message').html(response);
},
error: function(response){
$('#message').html('上传失败');
}
});
});
});
```
2. Java实现文件上传功能:
HTML代码:
```
<form action="upload.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
```
Java代码:
```
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
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;
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
out.println("<html><head><title>文件上传</title></head><body>");
out.println("<p>请上传文件</p>");
out.println("</body></html>");
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(1024 * 1024);
File tempDir = new File("d:/temp");
tempDir.mkdirs();
factory.setRepository(tempDir);
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
upload.setSizeMax(1024 * 1024 * 10);
try {
List<FileItem> items = upload.parseRequest(request);
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
FileItem item = iter.next();
if (!item.isFormField()) {
String fileName = item.getName();
String filePath = "d:/upload/" + fileName;
File uploadedFile = new File(filePath);
item.write(uploadedFile);
}
}
out.println("<html><head><title>文件上传</title></head><body>");
out.println("<p>上传成功</p>");
out.println("</body></html>");
} catch (FileUploadException e) {
out.println("<html><head><title>文件上传</title></head><body>");
out.println("<p>上传失败</p>");
out.println("</body></html>");
} catch (Exception e) {
out.println("<html><head><title>文件上传</title></head><body>");
out.println("<p>上传失败</p>");
out.println("</body></html>");
}
out.close();
}
}
```
注意:使用Java实现文件上传功能需要引入Apache Commons FileUpload库。
阅读全文