关于jspSmartUpload组件的saveAs函数报错

该博客介绍了如何使用Java Servlet和SmartUpload库来处理文件上传。首先,展示了一个HTML表单用于选择文件并提交。然后,`UploadwithServlet`类展示了初始化Servlet、设置文件类型和大小限制、上传文件以及保存到服务器的过程。在`web.xml`中配置了Servlet映射。最后,提到了可能出现的文件路径问题和解决建议。
摘要由CSDN通过智能技术生成

代码

  • upload.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
      <input type="file" name="fileName"> <br/>
<input type="submit" name ="upload" value="上传">
</form>
</body>
</html>
  • UploadwithServlet.java
package henu.file;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class UploadwithServlet extends HttpServlet {
	// 声明servletconfig对象,作为initialize()方法的参数
	ServletConfig servletconfig;

	public UploadwithServlet() {
		super();
	}

	// 初始化servletconfig对象
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		this.servletconfig = config;
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("进来了!");
		// 1.实例化一个SmartUpload对象
		SmartUpload su = new SmartUpload();
		// 2.初始化SmartUpolad对象
		try {
			su.initialize(servletconfig, request, response);
		} catch (ServletException e1) {
			e1.printStackTrace();
		}
		// 3.设置文件上传的 限制
		su.setAllowedFilesList("doc,docx,txt");
		// 单个文件最大字节数
		su.setMaxFileSize(3 * 1024 * 1024);
		// 文件总最大字节数
		su.setTotalMaxFileSize(12 * 1024 * 1024);
		// 4.使用upload上传

		try {
			su.upload();
		} catch (ServletException e2) {
			e2.printStackTrace();
		} catch (IOException e2) {
			e2.printStackTrace();
		} catch (SmartUploadException e2) {
			e2.printStackTrace();
		}
		// 5.文件保存
		Date curDate = new Date();
		long d = curDate.getTime();// long型
		// 创建request对象
		Request req = su.getRequest();
		// 获取上传文件
		// 获取全部上传文件
		Files files = su.getFiles();
		// 获取指定位置的文件
		File file = files.getFile(0);
		// 获取文件扩展名
		String extFile = file.getFileExt();
		String mainFile = String.valueOf(d);
		System.out.println("test");
		String filename = "/upload02/" + mainFile + "." + extFile;
		System.out.println(filename);
		try {
			file.saveAs(filename);
		} catch (IOException e1) {
			e1.printStackTrace();
		} catch (SmartUploadException e1) {
			e1.printStackTrace();
		}
	}
}
  • web.xml
   <!-- 文件上传 -->
   <servlet>
   	<servlet-name>upload</servlet-name>
   	<servlet-class>henu.file.UploadwithServlet</servlet-class>
   </servlet>
   <servlet-mapping>
   	<servlet-name>upload</servlet-name>
   	<url-pattern>/upload</url-pattern>
   </servlet-mapping>

编译文件位置
1.idea的文件传输渠道一般是在out文件夹
在这里插入图片描述
2. eclipse的默认web项目路径是在:
工作空间的metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps里面
如:

“D:\Workspace.metadata.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\ch8\upload\1652886156910.jpeg”


报错

  1. 若不存在所指定的文件夹,应按上述路径自己创建一个。(我的idea时而可以自己创建时而又报这个错误,求指教)
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Caseythekiwi_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值