日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

app上传头像处理Java_java后台加安卓端实现头像上传功能

發布時間:2024/1/23 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 app上传头像处理Java_java后台加安卓端实现头像上传功能 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、手機上傳壓縮后的圖片到服務器

2、后臺接收到圖片進行圖片重命名、保存

后臺代碼

package service;

import java.io.File;

import java.io.IOException;

import java.util.List;

import javax.servlet.Servlet;

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.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import bean.UpdateHeadBean;

import net.sf.json.JSONObject;

import utils.DbUtil;

@WebServlet("/UpdateHead")

public class UpdateHead extends HttpServlet implements Servlet{

private static final long serialVersionUID = 1L;

public UpdateHead() {

super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws

ServletException, IOException {

this.doPost(request, response);

response.getWriter().append("Served at: ").append(request.getContextPath());

}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,

IOException {

response.setCharacterEncoding("UTF-8");

DiskFileItemFactory factory = new DiskFileItemFactory();

ServletFileUpload upload = new ServletFileUpload(factory);

List items;

UpdateHeadBean updateHeadBean = new UpdateHeadBean();

try {

int uId = 0;

String pic =null;

String headName = null;

items = upload.parseRequest(request);//參數是HttpServletRequest對象

for (FileItem item : items){//遍歷所有客戶端提交的參數(包括文件域)

if(item.isFormField()){

if("uPic".equals(item.getFieldName())){//獲取手機端傳的參數(用戶id)

uId=Integer.valueOf(item.getString());

pic = DbUtil.queryPic(uId);

System.out.println("value:"+item.getString());

}

}else{

String key = item.getFieldName();//取出文件域的鍵

String value = item.getName();//取出文件域的值

long fileLen = item.getSize();//取出上傳文件的尺寸

String mimeType = item.getContentType();//取出上傳文件的類型

File f=new File("D:\\test\\");//文件地址(上傳后服務器硬盤地址)

f.mkdirs();

headName = System.currentTimeMillis()+value.substring(value.indexOf("."),value.length());

//保存圖片到服務器

item.write(new File(f+"\\"+headName));//保存上傳的文件到服務器本地

System.out.println("value:"+headName);

}

updateHeadBean.setStatus(0);

updateHeadBean.setMsg("上傳成功");

}

//根據用戶id保存圖片名稱到數據庫

DbUtil.saveHead(headName, uId);

//刪除原來的圖片文件

if(pic!=null&&pic!="1520848305600.jpg"){

File file = new File("d:\\test\\"+pic);

try {

if (file.exists()) {// 上面文件創建,已存在就刪除

boolean d = file.delete();

}

} catch (Exception e) {

e.printStackTrace();

}

}

} catch (Exception e) {

updateHeadBean.setStatus(1);

updateHeadBean.setMsg("上傳失敗");

e.printStackTrace();

}

//實體類轉為json字符串返回給客戶端

JSONObject msg = JSONObject.fromObject(updateHeadBean);

response.getWriter().write(msg.toString());

}

}

數據庫操作類

package utils;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import bean.Student;

public class DbUtil {

/*

* 保存數據

*/

public static Object saveData(String name,int age,String pss){

Connection connection = null;

PreparedStatement preparedStatement = null;

ResultSet rs = null;

Object retId = null;

try{

// 獲取連接

connection = JdbcUtils.getConnection();

// 準備sql語句

String sql = "INSERT INTO students(name,age,pss) VALUES(?,?,?)";

// 獲取PrepareStatement對象

preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);

// 填充占位符

preparedStatement.setString(1, name);

preparedStatement.setInt(2, age);

preparedStatement.setString(3, pss);

// 執行sql

preparedStatement.executeUpdate();

rs = preparedStatement.getGeneratedKeys();

if (rs.next()){

retId = rs.getObject(1);}

return retId;

}catch(SQLException ?e){

e.printStackTrace();

return null;

}finally{

JdbcUtils.releaseDB(connection, preparedStatement, null);

}

}

/*

* 查詢數據

*/

public static String queryData(String name){

Connection connection = null;

ResultSet rs = null;

try{

// 獲取連接

connection = JdbcUtils.getConnection();

Statement statement = connection.createStatement();

rs=statement.executeQuery("select * from students where name='"+name+"'");

if(rs.next()){

String userName = rs.getString("name");

return "注冊成功!";

}

}catch(SQLException ?e){

e.printStackTrace();

return "錯誤!";

}

finally{

try {

connection.close();

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return "錯誤!";

}

/*

* 查詢數據

*/

public static String queryPic(int id){

Connection connection = null;

ResultSet rs = null;

try{

// 獲取連接

connection = JdbcUtils.getConnection();

Statement statement = connection.createStatement();

rs=statement.executeQuery("select * from students where id='"+id+"'");

if(rs.next()){

String pic = rs.getString("pic");

return pic;

}

}catch(SQLException ?e){

e.printStackTrace();

return null;

}

finally{

try {

connection.close();

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return null;

}

public static Object queryData(String name,String pss,boolean flag){

Connection connection = null;

ResultSet rs = null;

try{

// 獲取連接

connection = JdbcUtils.getConnection();

Statement statement = connection.createStatement();

rs=statement.executeQuery("select * from students where name='"+name+"'and pss ='"+pss+"'");

if(rs.next()){

Object msg;

if(flag){

msg = rs.getInt("id");

}else{

msg = rs.getString("pic");

}

return msg;

}

}catch(SQLException ?e){

e.printStackTrace();

return null;

}

finally{

try {

connection.close();

rs.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return null;

}

/**

* 保存上傳的頭像

*/

public static boolean saveHead(String picName,int id){

Connection connection = null;

PreparedStatement preparedStatement = null;

try{

// 獲取連接

connection = JdbcUtils.getConnection();

// 準備sql語句 ?UPDATE Person SET FirstName = 'Fred' WHERE LastName = 'Wilson'

String sql = "update students set pic=? where id='"+id+"'";

// 獲取PrepareStatement對象

preparedStatement = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);

// 填充占位符

preparedStatement.setString(1, picName);

// 執行sql

preparedStatement.execute();

return true;

}catch(SQLException ?e){

e.printStackTrace();

return false;

}finally{

JdbcUtils.releaseDB(connection, preparedStatement, null);

}

}

}

3、使用tomcat映射圖片地址

打開Tomcat安裝文件夾——>conf——>Catalina——>localhost

創建xml文件 pic.xml (文件名必須與Context path的值一致),docBase與workDir的值為圖片所在文件夾,配置好后可在瀏覽器通過192.168.0.101:8080/pic/文件名 訪問圖片(192.168.0.101:8080為本機ip與tomcat端口號)

docBase="D:/test"

workDir="D:/test"

debug="0"

reloadable="true"

crossContext="true" />

總結

以上是生活随笔為你收集整理的app上传头像处理Java_java后台加安卓端实现头像上传功能的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。