下面是详细讲解“两种java文件上传实例讲解”的攻略:
一、基于Spring MVC框架的文件上传实例
1. 在Maven项目配置中添加以下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
2. 配置Spring MVC配置文件
在spring配置文件中,添加multipartResolver Bean,用来处理文件上传的请求。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000"/>
</bean>
3. 编写Controller类
@Controller
public class UploadFileController {
@RequestMapping(value="/uploadFile", method=RequestMethod.POST)
public String uploadFileHandler(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
byte[] bytes = file.getBytes();
// 创建文件保存路径
String path = "/tmp/" + file.getOriginalFilename();
File serverFile = new File(path);
BufferedOutputStream stream = new BufferedOutputStream(
new FileOutputStream(serverFile));
stream.write(bytes);
stream.close();
System.out.println("You have successfully uploaded " + file.getOriginalFilename());
} catch (Exception e) {
System.out.println("You failed to upload " + file.getOriginalFilename() + " => " + e.getMessage());
}
} else {
System.out.println("You failed to upload " + file.getOriginalFilename() + " because the file was empty.");
}
return "uploadFileSuccessView";
}
}
4. 编写前端页面
<form method="post" enctype="multipart/form-data" action="/SpringMVCExample/uploadFile">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
二、不依赖框架的文件上传实例
1. 前端页面
<form enctype="multipart/form-data" method="post" action="UploadServlet">
<input type="file" name="fileToUpload" id="fileToUpload">
<br>
<input type="submit" value="Upload">
</form>
2. 服务器端Java代码
@WebServlet("/UploadServlet")
@MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB
maxFileSize = 1024 * 1024 * 10, // 10MB
maxRequestSize = 1024 * 1024 * 50) // 50MB
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
PrintWriter out = response.getWriter();
try {
Part filePart = request.getPart("file");
inputStream = filePart.getInputStream();
File file = new File("C:/uploads/" + filePart.getSubmittedFileName());
outputStream = new FileOutputStream(file);
int read = 0;
final byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
System.out.println("New file " + file.getName() + " created at " + file.getAbsolutePath());
out.println("Received file=" + file.getName());
} catch (Exception e) {
out.println("Error=" + e.getMessage());
} finally {
if (inputStream != null) inputStream.close();
if (outputStream != null) outputStream.close();
if (out != null) out.close();
}
}
}
这是两种Java文件上传的实例,总体来说,使用Spring MVC框架上传文件更为简便,而不依赖框架的上传方式更为灵活,可以在任何Java Web项目中使用。我们可以根据实际需求,选择合适的文件上传方式。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:两种java文件上传实例讲解 - Python技术站