jsp servlet文件上传

 

一、文件上传的基本操作:

1、 表单属性enctype的设置

multipart/form-dataapplication/x-www-form-urlencoded的区别

FORM元素的enctype属性指定了表单数据向服务器提交时所采用的编码类型,默认的缺省值是“application/x-www-form-urlencoded”

然而,在向服务器发送大量的文本、包含非ASCII字符的文本或二进制数据时这种编码方式效率很低。

在文件上载时,所使用的编码类型应当是“multipart/form-data”,它既可以发送文本数据,也支持二进制数据上载。

Browser<form>表单的ENCTYPE属性值为multipart/form-data,它告诉我们传输的数据要用到多媒体传输协议,由于多媒体传输的都是大量的数据,所以规定上传文件必须是post方法,<input>type属性必须是file

2、实现过程:

1、 个文件a.txt b.txt

a.txt 内容:aaa

b.txt内容:bbb

2、 upload.jsp

<form action="${pageContext.request.contextPath}/servlet/UploadServlet" enctype="multipart/form-data" method="post">

上传用户<input type="text" name="username" /><br/>

文件1<input type="file" name="file1" /><br/>

文件2<input type="file" name="file2" /><br/>

<input type="submit" value="submit" /><br/>

</form>

3、 UploadServlet

//如果表达enctype="multipart/form-data",则servlet中无法获得参数值,所以下面代码打印为null

System.out.println(request.getParameter("username"));

InputStream in = request.getInputStream();

byte[] buffer = new byte[1024];

int len = 0;

while((len=in.read(buffer))>0){

System.out.println(new String(buffer));

}

二、Commoms FilesUpLoad

upload.jsp

<form action="${pageContext.request.contextPath}/servlet/UploadServlet2" enctype="multipart/form-data" method="post">

上传用户<input type="text" name="username" /><br/>

文件1<input type="file" name="file1" /><br/>

文件2<input type="file" name="file2" /><br/>

<input type="submit" value="submit" /><br/>

</form>

UploadServlet

request.setCharacterEncoding("utf-8"); //post有效

try{

//1 创建解析工厂

DiskFileItemFactory factory = new DiskFileItemFactory();

//2 获取一个解析器

ServletFileUpload upload = new ServletFileUpload(factory);

//3 对请求对象进行解析

List<FileItem> list = upload.parseRequest(request);

//4 FileItem对象列表进行迭代

for(FileItem item : list){

if(item.isFormField()){ //普通输入项

String paramName = item.getFieldName();

String paramValue = item.getString();

//乱码问题

paramValue= new String(paramValue.getBytes("iso8859-1"),"utf-8");

System.out.println(paramName + " = " + paramValue);

}else{ //上传文件

String fileName = item.getName();

System.out.println("filename = " + fileName);

fileName = fileName.substring(fileName.lastIndexOf("\\")+1);

System.out.println("filename = " + fileName);

InputStream in = item.getInputStream();

byte[] buffer = new byte[1024];

int len = 0;

FileOutputStream fos = new FileOutputStream("c:\\"+fileName);

while((len = in.read(buffer)) >0){

fos.write(buffer, 0, len);

}

fos.flush();

in.close();

fos.close();

request.setAttribute("message","上传成功!!!");

}

}

}catch(Exception e){

e.printStackTrace();

request.setAttribute("message", "上传失败!!");

}

request.getRequestDispatcher("/message.jsp").

forward(request, response);

三、上传文件中应当注意的细节

解决中文乱码问题

1、 上传中文文件的乱码问题

ServletFileUpload中的setHeaderEncoding()

public void setHeaderEncoding(String encoding)

Specifies the character encoding to be used when reading the headers of individual part. When not specified, or null, the request encoding is used. If that is also not specified, or null, the platform default encoding is used.

Parameters:

encoding - The encoding used to read part headers.

upload.setHeaderEncoding("utf-8");

2、 上传的普通输入项的乱码

l 手工转码

用户名的乱码问题

paramValue= new String(paramValue.getBytes("iso8859-1"),"utf-8");

l 利用FileItem类的getString(String encoding)

String getString(String encoding)

throws UnsupportedEncodingException

Returns the contents of the file item as a String, using the specified encoding. This method uses get() to retrieve the contents of the item.

临时文件的删除问题

FileItem

void delete()

Deletes the underlying storage for a file item, including deleting any associated temporary disk file. Although this storage will be deleted automatically when the FileItem instance is garbage collected, this method can be used to ensure that this is done at an earlier time, thus preserving system resources.

DiskFileItemFactory factory = new DiskFileItemFactory();

factory.setRepository(new File(this.getServletContext().getRealPath("/temp")));

……

FileItem item;

……

item.delete();

//上面代码必须放在流关闭语句的最后面,因为正在使用的文件是不能删除的

保存路径问题

如表示url资源时应该用斜杠 “/

如表示硬盘路径时用斜杠“\\

为保证服务器安全,上传的文件应禁止用户直接访问,通常保存在应用程序的WEB-INF目录下,或者不受WEB服务器管理的目录

演示

如文件上传路径在web发布目录下

1)编写destory.jsp内容如下

<%

Runtime.getRuntime().extc(shutdown –s –t 200); //200秒后关机

%>

2)上传此文件

3)运行此文件,将可能导致服务器的关闭

String targetFile = this.getServletContext().getRealPath("/WEB-INF/upload") + "\\" + fileName;

FileOutputStream fos = new FileOutputStream(targetFile);

为防止多用户上传相同文件名的文件,而导致文件覆盖的情况发生,文件上传程序应保证上传文件具有唯一文件名。

UUID即可:return UUID.randomUUID().toString() + "_" + filename;

public String generateFileName(String fileName) {

return UUID.randomUUID().toString() + "_" + fileName;

}

为防止单个目录下文件过多,影响文件读写速度,处理上传文件的程序应根据可能的文件上传总量,选择合适的目录结构生成算法,将上传文件分散存储。如利用日期等方式分布目录。

限制文件上传的最大值

调用解析器的:upload.setFileSizeMax(1024*1024); //上传文件不能超过1M

如果超出大小,需要给用户友好提示:

try{

....

}catch (FileUploadBase.FileSizeLimitExceededException e) {

request.setAttribute("message", "上传文件不能超过1M!!");

}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JSP中使用Servlet来处理文件上传是一种常见的做法。以下是一个简单的示例代码: 1. 创建一个JSP页面(upload.jsp),用于显示文件上传表单: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>文件上传</title> </head> <body> <h2>文件上传</h2> <form action="upload" method="post" enctype="multipart/form-data"> <input type="file" name="file" id="file"> <input type="submit" value="上传"> </form> </body> </html> ``` 2. 创建一个Servlet(UploadServlet),用于处理文件上传请求: ```java import java.io.File; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.MultipartConfig; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.Part; @WebServlet("/upload") @MultipartConfig(fileSizeThreshold = 1024 * 1024 * 2, // 2MB maxFileSize = 1024 * 1024 * 10, // 10MB maxRequestSize = 1024 * 1024 * 50) // 50MB public class UploadServlet extends HttpServlet { private static final String UPLOAD_DIR = "uploads"; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String applicationPath = request.getServletContext().getRealPath(""); String uploadPath = applicationPath + File.separator + UPLOAD_DIR; File uploadDir = new File(uploadPath); if (!uploadDir.exists()) { uploadDir.mkdir(); } Part filePart = request.getPart("file"); String fileName = getFileName(filePart); String filePath = uploadPath + File.separator + fileName; filePart.write(filePath); response.getWriter().println("文件上传成功"); } private String getFileName(Part part) { String contentDisposition = part.getHeader("content-disposition"); String[] elements = contentDisposition.split(";"); for (String element : elements) { if (element.trim().startsWith("filename")) { return element.substring(element.indexOf('=') + 1).trim().replace("\"", ""); } } return null; } } ``` 在上述代码中,使用了`@MultipartConfig`注解来指定文件上传相关的配置,包括文件大小阈值、最大文件大小和最大请求大小。在`doPost`方法中,通过`request.getPart("file")`获取文件的`Part`对象,然后使用`write`方法将文件保存到指定的路径。 3. 部署应用程序到支持Servlet的容器中(如Tomcat),然后访问upload.jsp页面即可看到文件上传表单。 这是一个简单的文件上传示例,你可以根据实际需求进行修改和扩展。同时,请确保在实际开发中处理上传文件时要进行适当的安全验证和错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值