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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

上传图片之后,截图

發布時間:2025/6/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 上传图片之后,截图 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

需求說明:

用戶在上傳頭像的時候,選擇自己的頭像上傳完畢,同時會進行截圖,并把四個點的坐標傳到后臺進行截圖。

功能點一:

上傳文件,同時命名:

apache的fileupload組件上傳。優點:可以同時上傳多個文件,也可以同時上傳文本域

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

response.setHeader("Cache-Control", "no-cache");

response.setContentType("text/plain; charset=UTF-8");

request.setCharacterEncoding("utf-8");

SessionManager sessionManager = SessionManager.getInstance();

UserSession user = (UserSession) sessionManager.getSession(request);

long suberId = 0;

if(user!=null){

if(user.getSuber()!=null){

l.debug("session中suber No null");

suberId = user.getSuber().getSuberId();

}

}

// 文件上傳部分

boolean isMultipart = ServletFileUpload.isMultipartContent(request);

String sourceName = null;// 源名

long size = 0L;// 大小

String fileName="";

String message = "上傳成功!";// 返回信息

/**

* 上傳文件的重命名

*/

String reName = "";

String suffix ="";

JSONObject jsonResult = new JSONObject();

if (isMultipart) {

try {

// 設置上傳(緩存目錄,編碼方式,文件大小限制)

DiskFileItemFactory factory = new DiskFileItemFactory();

factory.setRepository(new File(XingbookConfig.icon_tempPath));

ServletFileUpload upload = new ServletFileUpload(factory);

upload.setHeaderEncoding("utf-8");

upload.setSizeMax(g.uploadSizeMax);

// 上傳表單處理

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


Iterator<FileItem> iter = fileItems.iterator();

while (iter.hasNext()) {

FileItem item = iter.next();

if (item.isFormField()) {

} else {

// 獲得填充實例

StringBuffer pathTemp = new StringBuffer();

sourceName = item.getName().trim();// 保持原名上傳

suffix = sourceName.substring(sourceName.lastIndexOf('.') + 1).toLowerCase();// 后綴名

reName = suberId + ".jpg" ;// 重命名文件,當前用戶的id+上傳文件的后綴名

// 獲得目標路徑

pathTemp.delete(0, pathTemp.length());

pathTemp.append(XingbookConfig.icon_srcPath);

String iconPath = pathTemp.toString();

// 獲得新名稱完整路徑地址

pathTemp.delete(0, pathTemp.length());

pathTemp.append(iconPath).append(sourceName);

FileOperations operation = new FileOperations();

//判斷文件存在性,執行相應操作

//說明:文件的操作工具類,見《常用到的文件工具操作》

int result = operation.checkFileExists(pathTemp.toString());

if(result==2){

File fileOnServer = new File(iconPath, reName);

fileOnServer.delete();

}

Thread.sleep(1000);//留出時間往磁盤寫數據流

File fileOnServer = new File(iconPath, reName);

item.write(fileOnServer);

size = fileOnServer.length();

fileName = String.valueOf(suberId);

}

}

} catch (Exception e) {

message = "上傳過程中遇到問題,請聯系管理員 !";

l.error(e.getMessage());

e.printStackTrace();

}

} else {

message = "上傳過程中遇到問題,請聯系管理員";

}


try {

jsonResult.put("result", message);

jsonResult.put("fileName", fileName);

} catch (JSONException e) {

l.warn(e.getMessage());

}


PrintWriter out = response.getWriter();

out.print(jsonResult.toString());

}

功能點二:截圖:


public class IconAction extends BaseAction {

private int x;// X點坐標

private int y;// Y點坐標

private int w;// 寬度

private int h;// 高度


@Override

public String execute() throws Exception {

String suffix = ".jpg";

String _suffix = "";

long uid = sess.getSuber().getSuberId();

String srcPath = XingbookConfig.icon_srcPath + uid + suffix;

String targetPath = XingbookConfig.icon_targetPath + uid + suffix;

File file = new File(srcPath);

if(file.exists()){

_suffix = getFormatInFile(file);//文件上傳之后,將文件格式改為jpg格式,但是文件的讀取原格式仍為png,所以需要獲得文件的原文件格式

}

boolean result = cutImage(x, y, w, h, _suffix, srcPath, targetPath);

if (result) {

succe***esponse(null);

} else {

setMessage(R.ResultCode.USER_ICON_CUT, R.ResultCode.USER_ICON_CUT_MSG);

}

return MESSAGE;

}


@Override

public boolean needLogin() {

return true;

}


/**

* 圖片截取

* <p>

* Title: cutImage

* </p>

* <p>

* Description:

* </p>

*

* @param x

* ? ? ? ? ? ?截點橫坐標

* @param y

* ? ? ? ? ? ?截點縱坐標

* @param w

* ? ? ? ? ? ?長度

* @param h

* ? ? ? ? ? ?寬度

* @param suffix

* ? ? ? ? ? ?后綴名

* @param srcPath

* ? ? ? ? ? ?截圖的原始路徑

* @param targetPath

* ? ? ? ? ? ?截圖的目標路徑

* @return 是否截圖成功

*/

public boolean cutImage(int x, int y, int w, int h, String suffix, String srcPath, String targetPath) {

FileInputStream is = null;

ImageInputStream iis = null;

boolean result = false;

try {

is = new FileInputStream(srcPath);

Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(suffix);

ImageReader reader = it.next();

iis = ImageIO.createImageInputStream(is);

reader.setInput(iis, true);

ImageReadParam param = reader.getDefaultReadParam();

Rectangle rect = new Rectangle(x, y, w, h);

param.setSourceRegion(rect);

BufferedImage bi = reader.read(0, param);

ImageIO.write(bi, suffix, new File(targetPath));

result = true;

} catch (Exception e) {

e.printStackTrace();

} finally {

if (is != null) {

try {

is.close();

} catch (IOException e) {

l.debug("is關閉的時候出現異常");

}

}

if (iis != null) {

try {

iis.close();

} catch (IOException e) {

l.debug("iis關閉的時候出現異常");

}

}

}

return result;


}

/**

* 在上傳文件時,修改文件的格式為jpg,但是需要獲得文件的真實格式。

* @param f

* @return

*/

public static String getFormatInFile(File f){

return getFormatName(f);

}


private static String getFormatName(Object o) {

ImageInputStream iis;

try {

iis = ImageIO.createImageInputStream(o);

Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);

if(!iter.hasNext()){

return null;

}

ImageReader reader = iter.next();

iis.close();

return reader.getFormatName();

} catch (IOException e) {

e.printStackTrace();

}

return null;

}


public int getX() {

return x;

}


public void setX(int x) {

this.x = x;

}


public int getY() {

return y;

}


public void setY(int y) {

this.y = y;

}


public int getW() {

return w;

}


public void setW(int w) {

this.w = w;

}


public int getH() {

return h;

}


public void setH(int h) {

this.h = h;

}


轉載于:https://blog.51cto.com/1193452522/1279506

總結

以上是生活随笔為你收集整理的上传图片之后,截图的全部內容,希望文章能夠幫你解決所遇到的問題。

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