资源简介
做毕业设计的时候Android端要求把相关文件传到服务器端保存,目前这个就是用来接收文件上传请求的简单项目,目前就是在传送参数的时候中文会乱码,但是传送的文件不会乱码
代码片段和文件信息
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
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.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class MyUploadServlet
*/
@WebServlet(“/MyUploadServlet“)
public class MyUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private boolean isMultipart;
private String filePath;
private int maxFileSize = 50 * 1024;
private int maxMemSize = 10 * 1024;
private File file ;
private String targetPath;
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
filePath = “C:\\apache-tomcat-8.0.9\\webapps\\data\\“;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
response.getWriter().print(“getMethod:welcome!!!“);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request HttpServletResponse response) throws ServletException IOException {
isMultipart = ServletFileUpload.isMultipartContent(request);
response.setContentType(“text/html;charset=gb2312“);
// response.setCharacterEncoding(“utf-8“);
java.io.PrintWriter out = response.getWriter( );
if( !isMultipart ){
out.println(““);
out.println(““);
out.println(“tle>Servlet upload tle>“);
out.println(““);
out.println(““);
out.println(“No file uploaded
“);
out.println(““);
out.println(““);
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(maxMemSize);
// Location to save data that is larger than maxMemSize.
factory.setRepository(new File(“C:\\apache-tomcat-8.0.9\\webapps\\data\\temp\\“));
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum file size to be uploaded.
upload.setSizeMax( maxFileSize );
try{
// Parse the request to get file items.
List fileItems = upload.parseRequest(request);
// Process the uploaded file items
Iterator i = fileItems.iterator(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 834 2015-03-15 12:49 UploadTest\.classpath
文件 1043 2015-03-15 12:49 UploadTest\.project
文件 503 2015-03-15 12:49 UploadTest\.settings\.jsdtscope
文件 364 2015-03-15 12:49 UploadTest\.settings\org.eclipse.jdt.core.prefs
文件 482 2015-03-15 12:49 UploadTest\.settings\org.eclipse.wst.common.component
文件 345 2015-03-15 12:49 UploadTest\.settings\org.eclipse.wst.common.project.facet.core.xm
文件 49 2015-03-15 12:49 UploadTest\.settings\org.eclipse.wst.jsdt.ui.superType.container
文件 6 2015-03-15 12:49 UploadTest\.settings\org.eclipse.wst.jsdt.ui.superType.name
文件 4745 2015-03-15 13:29 UploadTest\build\classes\MyUploadServlet.class
文件 4584 2015-03-15 13:29 UploadTest\src\MyUploadServlet.java
文件 568 2015-03-15 13:22 UploadTest\WebContent\index.jsp
文件 39 2015-03-15 12:49 UploadTest\WebContent\me
文件 69002 2015-03-15 12:52 UploadTest\WebContent\WEB-INF\lib\commons-fileupload-1.3.1.jar
文件 185140 2015-03-15 12:52 UploadTest\WebContent\WEB-INF\lib\commons-io-2.4.jar
文件 927 2015-03-15 12:53 UploadTest\WebContent\WEB-INF\web.xm
目录 0 2015-03-15 13:55 UploadTest\WebContent\WEB-INF\lib
目录 0 2015-03-15 13:55 UploadTest\build\classes
目录 0 2015-03-15 13:55 UploadTest\WebContent\me
目录 0 2015-03-15 13:55 UploadTest\WebContent\WEB-INF
目录 0 2015-03-15 13:55 UploadTest\.settings
目录 0 2015-03-15 13:55 UploadTest\build
目录 0 2015-03-15 13:55 UploadTest\src
目录 0 2015-03-15 13:55 UploadTest\WebContent
目录 0 2015-03-15 13:55 UploadTest
----------- --------- ---------- ----- ----
268631 24
- 上一篇:模拟退火Java实现
- 下一篇:swagger中文bug的js补丁
相关资源
- 模拟退火Java实现
- 四种语言实现MD5加密(JSJSPJAVAASP及j
- 聊天室源代码(JSP+SERVLET)
- java语言实现求素数的原根
- java万年历的源代码显示农历及节假日
- java 分页源代码
- java swing实现的一个计算器
- JAVA计算器科学型+标准型
- JavaFx开发的经典魔塔
- centos7.4 opencv-341.jar + libopencv_java341.s
- java图像用户界面实现的通讯录程序
- Java EE期末考试试题
- Java新闻发布系统源代码
- java的ftp客户端实验报告,有代码
- Java万年历(代码及运行结果)
- 仿QQ登陆界面java程序设计
- 简易网上支付系统,基于jsp的实现,
- 带权图的多种算法有向图,无向图,
- net.mindview.util.*
- 找出一个二维数组的鞍点,即该位置
- Java实现密钥分配RSA+DES混合加密
- Java画面精美的简易聊天室
- JAVA绘制函数图像工具
- java打砖块
- 图书管理系统数据库课程设计java
- Java实现资源管理器
- java版中文汉字字频统计
- 一个模拟Windows的画图程序 java版
- Java学生签到系统源码
- java聊天室实验报告
评论
共有 条评论