Upload files using SmartUpload component

    The project I just started has been troubled by file uploading for a long time. I used the fileupload component to upload the code before, and the code refers to the Lonely Wolf Blog, but except for the file name, I don’t know how to read and store the other content in the form; so this time, the smartupload component is used for upload

    add.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'add.jsp' starting page</title>
  </head>
  
  <body>
    <form action="backpages/memberIntroduce/addDo.jsp" method="post" enctype="multipart/form-data">
    	姓名:<input type="text" name="name"><br>
    	简介:<input type="text" name="content"><br>
    	照片:<input type="file" name="photo"><br>
    	<input type="submit" value="提交">
    </form>
  </body>
</html>

     enctype must be "multipart/form-data";

     addDo.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.HQWeb.entity.*"%>
<%@ page import="com.HQWeb.dao.*"%>
<%@page import="com.jspsmart.upload.SmartUpload"%>
<%@page import="com.jspsmart.upload.File"%>
<%
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8");
	//Create smartupload component
	SmartUpload su = new SmartUpload();

	//Initialize the component context
	su.initialize(pageContext);
        //upload files
	su.upload ();

	out.println("Upload" + su.save("picture/memberPhoto") + "Files succeeded!!!"+"<br>");

	// get the form item
	//String name = su.getRequest().getParameter("name");
	//String content = su.getRequest().getParameter("content");
	String name = new String(su.getRequest()
			.getParameter("name").trim().getBytes(), "utf-8");
	String content = new String(su.getRequest()
			.getParameter("content").trim().getBytes(), "utf-8");

	// get a single file
	File file = su.getFiles().getFile(0);//A for loop can be used to get multiple files

	/ / Determine whether the current file has selected a file; if not, continue
	if (file.isMissing())
		System.out.println("No file selected");//If you use a for loop, continue

	//Set the location of the upload server
	String path = "/picture/memberPhoto";

	// get the name of the uploaded file
	String photo = file.getFileName();

	//path to upload file
	path = path + photo;

	//save document
	file.saveAs(path, SmartUpload.SAVE_VIRTUAL);
	System.out.println(photo);
	Member member = new Member();
	member.setName(name);
	member.setContent(content);
	member.setPhoto(photo);

	if (MemberDao.insert(member)) {
		out.println("Successful operation, return list after 3 seconds");
		response.setHeader("refresh", "3;list.jsp");
	} else {
		out.println("Failed operation, return to list after 3 seconds");
		response.setHeader("refresh", "3;list.jsp");
	}
%>

     In the addDo.jsp page, because smartupload encapsulates all the content, request.getParameter cannot get the fields, and you need to use the encapsulated su.getRequest().getParameter("name");

     But there is another problem. When uploading a Chinese form and storing it in the database, it will display garbled characters. Using su.getRequest().getParameter("name"). trim().getBytes(), "utf-8"); can solve part of it, The garbled problem remains to be resolved

 

 

 

    Re-summarize the upload file process yourself:

1. Instantiate the SmartUpload object;

<!--[if !supportLists]--> 2,  2, <!--[endif]--> Initialize the formation context ( initialize ( pageContext ) method);

<!--[if !supportLists]--> 3,  3, <!--[endif]--> file upload ( upload () method);

<!--[if !supportLists]--> 4,  4, <!--[endif]-->Get the form item;

<!--[if !supportLists]--> 5,  5, <!--[endif]--> Get a file ( getFiles () returns the type of Files , and then use its getFile ( index ) method to get a single file);

<!--[if !supportLists]--> 6,  6, <!--[endif]--> Set server location

<!--[if !supportLists]--> 7,  7, <!--[endif]-->Get filename

<!--[if !supportLists]--> 8,  8, <!--[endif]--> Set the path; that is, there must be "/" between path+filename  path and file name, otherwise the file will be uploaded to pth path, and change the file name to the directory folder name + file name

<!--[if !supportLists]--> 9,  9, <!--[endif]-->Use file.saveAs(path, SmartUpload.SAVE_VIRTUAL); method to save the file to the specified directory

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326678278&siteId=291194637