在不用ssh或其他框架开发情况下,要进行传文件,在网上找了一大堆都是用小脚本在jsp页面上进行
处理的,现整理一份完全在servlet下用SmartUpload进行上传文件进行,
页面:uploadFile.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
<form action="UploadFileServlet?act=upload" name="uploadFrm"
enctype="multipart/form-data" method="post">选择文件路径:<input
type="file" name="fileName" id="fileName"><br>
用户名:<input type="text" name="userName" id="userName"><br>
<input type="submit" value="提交"><br>
</form>
</center>
</body>
</html>
servlet文件:
package com.uploadFileTest.servlet;
import java.io.IOException;
import java.util.Random;
import javax.servlet.Servlet;
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.SmartFile;
import com.jspsmart.upload.SmartFiles;
import com.jspsmart.upload.SmartRequest;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
/**
*
* @author chende
*
*/
public class UploadFileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ServletConfig config;
/**
* @see HttpServlet#HttpServlet()
*/
public UploadFileServlet() {
super();
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
/**
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String urlType = request.getParameter("act");
if (urlType != null) {
if (urlType.equals("upload")) {
String userName = this.getPicAndRst(config, request, response);// 调用上传文件
String[] values = userName.split(",");
request.setAttribute("userne", values[0]);
request.setAttribute("file", values[1]);
request.getRequestDispatcher("result.jsp").forward(request,
response);
}
}
}
/**
* 注册并上传图片用户登录
*
* @param config
* ServletConfig
* @param request
* HttpServletRequest
* @param response
* HttpServletResponse
* @throws ServletException
* @throws IOException
*/
public String getPicAndRst(ServletConfig config,
HttpServletRequest request, HttpServletResponse response) {
SmartUpload sud = new SmartUpload();// 初始化一个上传组件
sud.setCharset("UTF-8");
String filePath = config.getServletContext().getRealPath("/")
+ "images";// 上传文件保存的路径
try {
sud.initialize(config, request, response);// 传入提交的初始参数
sud.setMaxFileSize(2 * 1024 * 1024);// 文件大小
sud.upload();
SmartFiles files = sud.getFiles();
SmartFile file = files.getFile(0);
//file.setCharset("UTF-8");
String filepathName=file.getFilePathName();
String fileName =file.getFileName();
file.saveAs(filePath + "/" + fileName, SmartUpload.SAVE_PHYSICAL);
SmartRequest srst = sud.getRequest();
String userName = srst.getParameter("userName");// 用户输入的用户名S
if (userName != null && !userName.trim().equals("")) {
return userName + "," + filePath + "/" + fileName;
}
} catch (ServletException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SmartUploadException e) {
e.printStackTrace();
}
return null;
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
跳转页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<center>
您提交的用户为:${requestScope.userne}<br>
您保存文件路径为:${requestScope.file}<br>
</center>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>UploadFileDemo</display-name>
<welcome-file-list>
<welcome-file>uploadFile.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description>上传图片</description>
<display-name>UploadFileServlet</display-name>
<servlet-name>UploadFileServlet</servlet-name>
<servlet-class>com.uploadFileTest.servlet.UploadFileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadFileServlet</servlet-name>
<url-pattern>/UploadFileServlet</url-pattern>
</servlet-mapping>
</web-app>
SmartUpload.jar要修改下字符编码,不然文件名和其他参数为中文会乱码
参照:
http://dev.firnow.com/course/3_program/java/javajs/20090405/164281.html