淘忆项目之分享界面服务器端的修正归纳

淘忆项目之分享界面服务器端的修正归纳

分享界面:

主要实现数据的上传,就是将需要分享的物品的相关信息的提交,包括图片,名称,标签,详细叙述。图片提交到update的目录下,文字内容和图片的名字提交到数据库。

第一步:用photoshop设计界面。

 

第二步:服务器端需要先接受图片的传输,然后是接受文字信息填到数据库中。

这里我在网上找了相关资料然后总结了一下。

首先建立com.elaine.picUploadServelt包,然后建立PicUploadServlet.java

然后通过DiskFileItemFactory来传输图片,存到upload目录下。

package com.elaine.picUpload;

 

import java.io.File;

import java.io.IOException;

import java.util.List;

 

import javax.servlet.ServletException;

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;

 

public class PicUploadServlet extends HttpServlet {

 

/**

 *

 */

private static final long serialVersionUID = 1L;

 

/**

 * Constructor of the object.

 */

public PicUploadServlet() {

super();

}

 

/**

 * Destruction of the servlet. <br>

 */

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

 

/**

 * The doGet method of the servlet. <br>

 *

 * This method is called when a form has its tag value method equals to get.

 *

 * @param request

 *            the request send by the client to the server

 * @param response

 *            the response send by the server to the client

 * @throws ServletException

 *             if an error occurred

 * @throws IOException

 *             if an error occurred

 */

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

 

this.doPost(request, response);

}

 

/**

 * The doPost method of the servlet. <br>

 *

 * This method is called when a form has its tag value method equals to

 * post.

 *

 * @param request

 *            the request send by the client to the server

 * @param response

 *            the response send by the server to the client

 * @throws ServletException

 *             if an error occurred

 * @throws IOException

 *             if an error occurred

 */

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

request.setCharacterEncoding("utf-8");

DiskFileItemFactory factory = new DiskFileItemFactory();

String path = request.getRealPath("/upload");

File file = new File(path);

if (!file.exists()) {

file.mkdirs();

}

factory.setRepository(new File(path));

factory.setSizeThreshold(1024 * 1024);

ServletFileUpload upload = new ServletFileUpload(factory);

try {

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

for (FileItem item : list) {

String name = item.getFieldName();

if (item.isFormField()) {

String value = item.getString();

request.setAttribute(name, value);

} else {

String value = item.getName();

int start = value.lastIndexOf("\\");

String filename = value.substring(start + 1);

request.setAttribute(name, filename);

item.write(new File(path, filename));

System.out.println("上传成功" + filename);

response.getWriter().print(filename);

}

}

 

} catch (Exception e) {

System.out.println("上传失败");

response.getWriter().print("上传失败" + e.getMessage());

}

 

}

 

/**

 * Initialization of the servlet. <br>

 *

 * @throws ServletException

 *             if an error occurs

 */

public void init() throws ServletException {

// Put your code here

}

 

}

第三步,对于文字的处理:

首先建立servicedaoaction类来处理数据。

ThingService.java中写入一个方法:

package com.elaine.thing.service;

 

import java.util.List;

 

public interface ThingService {

public boolean addThing(List<Object> params);

}

然后再thingDao.java中写入:

package com.elaine.thing.dao;

 

import java.util.List;

 

import com.elaine.jdbc.JdbcUtils;

import com.elaine.thing.service.ThingService;

 

public class ThingDao implements ThingService{

 

JdbcUtils jdbcUtils;

public ThingDao() {

// TODO Auto-generated constructor stub

jdbcUtils=new JdbcUtils();

}

 

@Override

public boolean addThing(List<Object> params) {

// TODO Auto-generated method stub

boolean flag=false;

try {

String sql="insert into thingInfo (thingId,userId,thingName,thingTip,thingContent,thingPic,submitTime,thingGoodNum,thingMesNum) values(?,?,?,?,?,?,?,?,?)";

jdbcUtils.getConnection();

flag = jdbcUtils.updateByPreparedStatement(sql, params);

} catch (Exception e) {

// TODO: handle exception

}finally{

jdbcUtils.releaseConn();

}

return flag;

}

 

}

再然后是对客户端数据传输来的数据的处理和返回信息:

thingAction.java中写入:

package com.elaine.thing.action;

 

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import com.elaine.thing.dao.ThingDao;

import com.elaine.thing.service.ThingService;

import com.elaine.tools.UUIDtools;

import com.elaine.tools.jsonTool;

 

public class ThingAction extends HttpServlet {

 

/**

 *

 */

private static final long serialVersionUID = 1L;

private ThingService service;

 

/**

 * Constructor of the object.

 */

public ThingAction() {

super();

}

 

/**

 * Destruction of the servlet. <br>

 */

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

 

/**

 * The doGet method of the servlet. <br>

 *

 * This method is called when a form has its tag value method equals to get.

 *

 * @param request

 *            the request send by the client to the server

 * @param response

 *            the response send by the server to the client

 * @throws ServletException

 *             if an error occurred

 * @throws IOException

 *             if an error occurred

 */

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

this.doPost(request, response);

}

 

/**

 * The doPost method of the servlet. <br>

 *

 * This method is called when a form has its tag value method equals to

 * post.

 *

 * @param request

 *            the request send by the client to the server

 * @param response

 *            the response send by the server to the client

 * @throws ServletException

 *             if an error occurred

 * @throws IOException

 *             if an error occurred

 */

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

 

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

response.setContentType("text/html;charset=utf-8");

PrintWriter out = response.getWriter();

String action_flag = request.getParameter("action_flag");

if (action_flag.equals("addThing")) {

addThing(request, response);

}

out.flush();

out.close();

}

 

private void addThing(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

// TODO Auto-generated method stub

PrintWriter out=response.getWriter();

String userId=request.getParameter("userId");

String thingName=request.getParameter("thingName");

String thingTip=request.getParameter("thingTip");

String thingContent=request.getParameter("thingContent");

String thingPicName=request.getParameter("thingPicName");

String submitTime=request.getParameter("submitTime");

Integer thingGoodNum=0;//初始化数据

Integer thingMesNum=0;//初始化数据

List<Object> params=new ArrayList<Object>();

params.add(UUIDtools.getUUID()); //添加thingId

params.add(userId);

params.add(thingName);

params.add(thingTip);

params.add(thingContent);

params.add(thingPicName);

params.add(submitTime);

params.add(thingGoodNum);

params.add(thingMesNum);

boolean flag=service.addThing(params);

if(flag){

String addThingMsg="发布成功";

String addThingMsgJson=jsonTool.creataJsonString("msg", addThingMsg);

out.print(addThingMsgJson);

}else{

String addThingMsg="发布失败";

String addThingMsgJson=jsonTool.creataJsonString("msg", addThingMsg);

out.print(addThingMsgJson);

}

out.flush();

out.close();

}

 

/**

 * Initialization of the servlet. <br>

 *

 * @throws ServletException

 *             if an error occurs

 */

public void init() throws ServletException {

// Put your code here

service = new ThingDao();

}

 

}

服务器端的处理不难,就是对数据的添加操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值